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 <[email protected]>

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

Co-authored-by: Edoardo Gallo <[email protected]>

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

Co-authored-by: Edoardo Gallo <[email protected]>

* fix typo

* type in changeset

---------

Co-authored-by: Edoardo Gallo <[email protected]>
  • Loading branch information
iAmmar7 and Edoardo Gallo authored Aug 28, 2023
1 parent 5c898c2 commit e3f84f5
Show file tree
Hide file tree
Showing 33 changed files with 1,507 additions and 691 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 e3f84f5

Please sign in to comment.