Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active June 4, 2018 16:27
Show Gist options
  • Save bookercodes/66a89d36ba49c24e16e88debdb95682f to your computer and use it in GitHub Desktop.
Save bookercodes/66a89d36ba49c24e16e88debdb95682f to your computer and use it in GitHub Desktop.
// ./src/Chat.js
import React, { Component } from 'react'
import { ChatManager, TokenProvider } from '@pusher/chatkit'
import MessageList from './MessageList'
+import SendMessageForm from './SendMessageForm'
class Chat extends React.Component {
state = {
currentUser: null,
currentRoom: {},
messages: []
}
componentDidMount() {
const chatkit = new ChatManager({
instanceLocator: 'YOUR INSTANCE LOCATOR',
userId: this.props.currentId,
tokenProvider: new TokenProvider({
url: 'YOUR TOKEN PROVIDER URL'
})
})
chatkit
.connect()
.then(currentUser => {
this.setState({ currentUser })
return currentUser.subscribeToRoom({
roomId: 8434070, // Replace with YOUR ROOM ID
messageLimit: 100,
hooks: {
onNewMessage: message => {
this.setState({
messages: [...this.state.messages, message]
})
}
}
})
})
.then(currentRoom => {
this.setState({ currentRoom })
})
.catch(error => console.error('error', error))
}
+ onSend = text => {
+ this.state.currentUser.sendMessage({
+ text,
+ roomId: this.state.currentRoom.id
+ })
+ }
render() {
return (
<div className="wrapper">
<div className="chat">
<MessageList messages={this.state.messages} />
+ <SendMessageForm onSend={this.onSend} />
</div>
</div>
)
}
}
export default Chat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment