Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Last active September 24, 2017 16:34
Show Gist options
  • Save ernestofreyreg/dd84a72e986bc8415c9c05a0605bdcf6 to your computer and use it in GitHub Desktop.
Save ernestofreyreg/dd84a72e986bc8415c9c05a0605bdcf6 to your computer and use it in GitHub Desktop.
const RTM = require('satori-rtm-sdk')
// create an RTM client instance
const rtm = new RTM('YOUR_ENDPOINT', 'YOUR_APPKEY')
// create a new subscription with 'your-channel'
const channel = rtm.subscribe('YOUR_CHANNEL', RTM.SubscriptionMode.SIMPLE)
// add channel data handlers
// channel receives any published message
channel.on('rtm/subscription/data', pdu => {
const messages = pdu.body.messages
messages.forEach(console.log)
})
// client enters 'connected' state
rtm.on('enter-connected', () => {
const payload = {key: 'value'}
rtm.publish('YOUR_CHANNEL', payload)
})
// client receives any PDU and PDU is passed as a parameter
rtm.on('data', pdu => {
if (pdu.action.endsWith('/error')) {
rtm.restart()
}
})
// start the client
rtm.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment