Skip to content

Instantly share code, notes, and snippets.

@davidsaccavino
Created August 17, 2018 20:38
Show Gist options
  • Save davidsaccavino/6f4c4ecdfa024371a9a657bb440a64e0 to your computer and use it in GitHub Desktop.
Save davidsaccavino/6f4c4ecdfa024371a9a657bb440a64e0 to your computer and use it in GitHub Desktop.
/*
this is the server.js file
*/
io.on('connection', (client) => {
client.on('helloUser', () => {
console.log('sending');
client.emit('hello', 'hello world')
});
});
const port = 8000;
io.listen(port);
console.log('listening on port ', port);
/*
this is the main app.js file
*/
import React, { Component } from 'react';
import openSocket from 'socket.io-client';
const socket = openSocket('http://localhost:8000');
class App extends Component {
constructor(props) {
super(props);
this.state = {
hello: "not hello"
};
socket.emit('helloUser');
socket.on('hello', msg => this.setState({hello: msg}));
}
render() {
return (
<div>
<p>
{this.state.hello}
</p>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment