Skip to content

Instantly share code, notes, and snippets.

@vinnyoodles
Created December 25, 2016 04:21
Show Gist options
  • Save vinnyoodles/52c4ce2d7681f2907692c7f71f6550df to your computer and use it in GitHub Desktop.
Save vinnyoodles/52c4ce2d7681f2907692c7f71f6550df to your computer and use it in GitHub Desktop.
React Native Socket.io User
// Client side
determineUser() {
AsyncStorage.getItem('userId')
.then((userId) => {
if (userId) {
this.socket.emit('i-dont-need-an-id');
} else {
this.socket.emit('i-need-id');
this.socket.on('here-is-your-id', (id) => {
AsyncStorage.setItem('userId', id);
// Force a rerender in the React component
this.setState({ id });
});
}
});
}
// Server side
socket.on('i-need-id', () => {
// Create a document in the db and grab that id.
var user = db.collection('users').insert({});
socket.emit('here-is-your-id', user._id);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment