Last active
December 25, 2016 04:26
-
-
Save vinnyoodles/f2adb22bb096b8d3abfd45b893d1e550 to your computer and use it in GitHub Desktop.
React Native Socket.io Broadcast Message
This file contains 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
// Server side | |
socket.on('message', (message) => { | |
// Save the message document in the `messages` collection. | |
db.collection('messages').insert(message); | |
// The `broadcast` allows us to send to all users but the sender. | |
socket.broadcast.emit('message', message); | |
}); | |
// Client side | |
socket.on('message', (message) => { | |
var oldMessages = this.state.messages; | |
// React will automatically rerender the component when a new message is added. | |
this.setState({ messages: oldMessages.concat(message) }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment