Skip to content

Instantly share code, notes, and snippets.

@khle
Created January 9, 2016 07:13
Show Gist options
  • Save khle/ef1b252ec8dca3294bce to your computer and use it in GitHub Desktop.
Save khle/ef1b252ec8dca3294bce to your computer and use it in GitHub Desktop.
var Main = React.createClass({
getInitialState() {
//already shown
},
componentDidMount() {
var socket = io();
var props = this.props;
var users = this.state.users;
var messages = this.state.messages;
var self = this;
var socketIdStream = Rx.Observable.create(observer => {
socket.on('my socketId', data => { observer.onNext(data); });
});
socketIdStream.subscribe(data => {
socket.emit('client connect', {
nickname: props.nickname,
socketId: data.socketId,
connectTime: data.connectTime
});
});
var socketAllUsersStream = Rx.Observable.create(observer => {
socket.on('all users', data => { observer.onNext(data); });
});
socketAllUsersStream.subscribe(data => {
self.setState({users: data});
});
var socketMessageStream = Rx.Observable.create(observer => {
socket.on('message', data => { observer.onNext(data); });
});
socketMessageStream.subscribe(data => {
messages.push(data);
self.setState(messages); //data is {'message': text, 'who': nickname, 'timestamp': Date.now}
});
},
render() {
//already shown
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment