Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Last active September 24, 2017 17:24
Show Gist options
  • Save ernestofreyreg/6534b0cc7f2e029ec28359d057615d62 to your computer and use it in GitHub Desktop.
Save ernestofreyreg/6534b0cc7f2e029ec28359d057615d62 to your computer and use it in GitHub Desktop.
import React from 'react'
import RTM from 'satori-rtm-sdk'
class Index extends React.Component {
state = {}
componentDidMount () {
const rtm = new RTM('YOU_ENDPOINT', 'YOUR_APPKEY')
const channel = rtm.subscribe('YOUR_CHANNEL', RTM.SubscriptionMode.SIMPLE)
channel.on('rtm/subscription/data', pdu => {
const messages = pdu.body.messages
this.updateUI(messages)
})
rtm.on('data', pdu => {
if (pdu.action.endsWith('/error')) {
rtm.restart()
}
})
rtm.start()
this.setState({rtm, channel})
}
componentWillUnmount () {
this.state.rtm.stop()
}
updateUI = messages => {
if (messages && messages.length > 0) {
const lastMessage = messages[messages.length - 1]
this.setState(lastMessage)
}
}
render () {
const { talk } = this.state
return (
<div>{talk || 'Hello'}</div>
)
}
}
export default Index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment