Last active
September 24, 2017 17:24
-
-
Save ernestofreyreg/6534b0cc7f2e029ec28359d057615d62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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