Skip to content

Instantly share code, notes, and snippets.

@sohelamin
Created April 22, 2017 18:00
Show Gist options
  • Save sohelamin/91c23bd6df34e895414578d4e2dc788b to your computer and use it in GitHub Desktop.
Save sohelamin/91c23bd6df34e895414578d4e2dc788b to your computer and use it in GitHub Desktop.
Socket.io
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/chat', function(req, res){
res.sendFile(__dirname + '/chat.html');
});
var people = {};
io.on('connection', function(socket) {
socket.on('online user', function(user) {
people[user] = socket.id;
});
socket.on('chat message', function(msg, user) {
socket.to(people[user]).emit('chat message', msg);
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment