Skip to content

Commit

Permalink
PubSub and Chat namespace with new interface (#814)
Browse files Browse the repository at this point in the history
* Task namespace with new interface

* taskworker include

* extend task from applyeventlisteners

* base namespace class to handle the listen method

* topic attach to event name

* type update

* remove older Task api

* refactor and e2e test case

* utility function to prefix the event

* PubSub namespace with new interface

* new interface for the Chat API

* fix stack tests

* include e2e test for PubSub API

* e2e test case for Chat interface

* test disconnected client

* unit tests for Base classes

* Unit tests for the Task class

* fix TS for the Task class unit test

* unit tests for PubSub and Chat API classes

* include changeset

* Update packages/realtime-api/src/chat/workers/chatWorker.ts

Co-authored-by: Edoardo Gallo <edoardo@signalwire.com>

* Update packages/realtime-api/src/chat/workers/chatWorker.ts

Co-authored-by: Edoardo Gallo <edoardo@signalwire.com>

* Update packages/realtime-api/src/pubSub/workers/pubSubWorker.ts

Co-authored-by: Edoardo Gallo <edoardo@signalwire.com>

* fix typo

* type in changeset

---------

Co-authored-by: Edoardo Gallo <edoardo@signalwire.com>
  • Loading branch information
iAmmar7 and Edoardo Gallo committed Dec 5, 2023
1 parent bc250fc commit b1e211c
Show file tree
Hide file tree
Showing 33 changed files with 1,506 additions and 689 deletions.
76 changes: 76 additions & 0 deletions .changeset/hip-bobcats-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
'@signalwire/realtime-api': major
'@signalwire/core': major
---

New interface for PubSub and Chat APIs

The new interface contains a single SW client with Chat and PubSub namespaces
```javascript
import { SignalWire } from '@signalwire/realtime-api'

(async () => {
const client = await SignalWire({
host: process.env.HOST,
project: process.env.PROJECT,
token: process.env.TOKEN,
})

// Attach pubSub listeners
const unsubHomePubSubListener = await client.pubSub.listen({
channels: ['home'],
onMessageReceived: (message) => {
console.log('Message received under the "home" channel', message)
},
})

// Publish on home channel
await client.pubSub.publish({
content: 'Hello There',
channel: 'home',
meta: {
fooId: 'randomValue',
},
})

// Attach chat listeners
const unsubOfficeChatListener = await client.chat.listen({
channels: ['office'],
onMessageReceived: (message) => {
console.log('Message received on "office" channel', message)
},
onMemberJoined: (member) => {
console.log('Member joined on "office" channel', member)
},
onMemberUpdated: (member) => {
console.log('Member updated on "office" channel', member)
},
onMemberLeft: (member) => {
console.log('Member left on "office" channel', member)
},
})

// Publish a chat message on the office channel
const pubRes = await client.chat.publish({
content: 'Hello There',
channel: 'office',
})

// Get channel messages
const messagesResult = await client.chat.getMessages({
channel: 'office',
})

// Get channel members
const getMembersResult = await client.chat.getMembers({ channel: 'office' })

// Unsubscribe pubSub listener
await unsubHomePubSubListener()

// Unsubscribe chat listener
await unsubOfficeChatListener()

// Disconnect the client
client.disconnect()
})();
```
Loading

0 comments on commit b1e211c

Please sign in to comment.