why deepstreamHub? compare us getting started faq
use cases
products
developers
company
blog contact

Events are deepstreamHub’s publish-subscribe mechanism. Clients and backend processes can subscribe to event-names (sometimes also called “topics” or “channels”) and receive messages published by other endpoints.

Events are non-persistent, one-off messages. For persistent data, please use records.

Methods

client.event.subscribe( event, callback )

argument type optional description
event String false an eventname
callback Function false A function that will be called whenever the event is emitted

Subscribes to an event. Callback will receive the data passed to emit()

// Client A
client.event.subscribe('news/sports', data => {
  // handle published data
})

// Client B
client.event.emit('news/sports', { some: 'data' })

client.event.unsubscribe( event, callback )

argument type optional description
event String false an eventname
callback Function true The callback that was previously registered with subscribe. If the callback is omitted, all listeners for the event will be removed

Unsubscribes from an event that was previously registered with subscribe(). This stops a client from receiving the event.

// Client A
client.event.unsubscribe('news/politics', callback)

client.event.emit( event, data )

argument type optional description
event String false an eventname
data Mixed true Any serialisable data ( Objects, Strings, Numbers... ) that will be sent with the event

Sends the event to all subscribed clients

// Client B
client.event.emit('notifications', 'Maria just came online')

client.event.listen(pattern, callback)

opensource / enterprise
argument type optional description
pattern String (regex) false The pattern to match events which subscription status you want to be informed of
callback Function false function that will be called whenever an event has been initially subscribed to or is no longer subscribed. Arguments are (String) match, (Boolean) isSubscribed, and response (Object)

Registers the client as a listener for event subscriptions made by other clients. This is useful to create active data providers - processes that only send events if clients are actually interested in them. You can find more about listening in the events tutorial.

client.event.listen('^news/.*', (eventName, isSubscribed, response) => {
  // see tutorial for more details
})

client.event.unlisten(pattern)

opensource / enterprise
argument type optional description
pattern String (regex) false The pattern that has been previously listened to

Remove the listener added via listen(pattern,listener). This will remove the provider as the active provider and allow another provider to take its place.

client.event.unlisten("users/*", listener);