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' }})
ParamType Description
eventNamestringThe 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) => {})
ParamType Description
eventNamestringThe name of the event you want to subscribe to.
onEventReceivedFunction(payload)Callback called whenever a published event matches the eventName

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

The scope param is used to determine which room should receive the published event.

ParamTypeDescription
typeStringorganization will send to every event, event will send to every session of the event, session will send to a specific session
event_iduuid(optional) The event to which you want to publish (required if you use session or event type)
session_iduuid(optional) The session to which you want to publish (required if you use session type)

payload.event

The event payload will contain what makes your event specific.

ParamTypeDescription
eventNameStringThis is the key that needs to match the PubSub.subscribe(key, (payload) => {}) subscription in your plugin
dataobjectAn 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 => {})

What’s Next
Did this page help you?