var app = require('express')();
var io = require('socket.io');
- Init http middleware that bind to the express server
var http = require('http').Server(app)
- So we rewrite the io initialization into this
var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('a client is connected')
socket.on('disconnect', function(socket){
console.log('client disconnected');
})
socket.on('chat message', function(msg, socket){
console.log('message from client: ', msg)
})
})
io.emit('chat message', 'this is my message')
ScreenShot
The client will looked like this: