Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yangga
Last active March 4, 2019 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangga/a4b3f16e24d959a1085f1319dfeb8e66 to your computer and use it in GitHub Desktop.
Save yangga/a4b3f16e24d959a1085f1319dfeb8e66 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello Socket!');
});
var server = require('http').createServer(app).listen(3000, function() {
console.log(`Listening on port 3000 with http`);
});
const socketio = require('socket.io')
const io = socketio(server)
io.on('connection', (socket) => {
console.log('Client connected');
// echo received chat message
socket.on('chat', function(msg){
console.log('Recv chat:', msg)
io.emit('chat', msg);
});
socket.on('disconnect', () => {
console.log('Client disconnected');
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment