PubSub
Even though this API does have any direct UI impact, it is actually the foundation of most Plugins.
publish()
Publish an event to any connected Subscriber via websockets.
Can be used to communicate to other people in the Room for use cases such as :
chat, dynamic content, video games, breakout rooms, polls, etc.
This API is also usable via an HTTP Call to publish from any other tool (curl, server, Zapier, IFTTT, etc).
Subscribers are not shared between rooms.
usage :
PubSub.publish('say-hello', { data: { custom: 'payload' }})
Param | Type | Description |
---|---|---|
eventName | string | The name of the event you want to publish. |
data | { data: {} } | Custom payload to send to the subscribers |
subscribe()
Subscribe to a Published event.
Can be used to trigger any action whenever an event is Published in use cases such as :
chat, dynamic content, video games, breakout rooms, polls, etc.
usage :
PubSub.subscribe('say-hello', (message) => {})
Param | Type | Description |
---|---|---|
eventName | string | The name of the event you want to subscribe to. |
onEventReceived | Function(payload) | Callback called whenever a published event matches the eventName |
POST api/v1/publish
POST api/v1/publish
The HTTP API can be used to publish events from outside of the room. This is useful for achieving workflows that depend on events happening in realtime: a new customer subscription, your favorite team scored, your app has been deployed, etc.
usage :
POST api/v1/pub_subs
Host: plugins.livestorm.co
Content-Type: application/json
Authorization: {{ API_TOKEN }}
{
"scope": {
"type": "session",
"event_id": "xxx",
"session_id": "xxx"
},
"payload": {
"event": {
"eventName": "hello",
"data": {
"foo": "bar"
}
}
}
}
You can catch this call in the room using:
const eventName = 'hello' // This corresponds to the "eventName" key in the request body.
PubSub.subscribe(eventName, (message) => {
console.log(message) // => { foo: "bar" }
})
There are 2 important keys in the request's body:
scope
scope
The scope param is used to determine which room should receive the published event.
Param | Type | Description |
---|---|---|
type | String | organization will send to every event, event will send to every session of the event, session will send to a specific session |
event_id | uuid | (optional) The event to which you want to publish (required if you use session or event type) |
session_id | uuid | (optional) The session to which you want to publish (required if you use session type) |
payload.event
payload.event
The event payload will contain what makes your event specific.
Param | Type | Description |
---|---|---|
eventName | String | This is the key that needs to match the PubSub.subscribe(key, (payload) => {}) subscription in your plugin |
data | object | An object containing the payload of your event, can be anything. You will receive the given object as the payload of the callback function of PubSub.subscribe(key, payload => {}) |
Updated 8 days ago