Skip to content

Instantly share code, notes, and snippets.

@vinnyoodles
Last active December 25, 2016 04:26
Show Gist options
  • Save vinnyoodles/f2adb22bb096b8d3abfd45b893d1e550 to your computer and use it in GitHub Desktop.
Save vinnyoodles/f2adb22bb096b8d3abfd45b893d1e550 to your computer and use it in GitHub Desktop.
React Native Socket.io Broadcast Message
// 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