Created
December 25, 2016 04:21
-
-
Save vinnyoodles/52c4ce2d7681f2907692c7f71f6550df to your computer and use it in GitHub Desktop.
React Native Socket.io User
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
// 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