socket io tutorial - Installation or Setup - socket io android - socket io client
First, install Socket io module in node.js application.
npm install Socket io --saveclick below button to copy the code. By Socket tutorial team
Basic HTTP Setup
- The following example attaches Socket io to a plain node.js HTTP server listening on port 3000.
var server = require('http').createServer();
var io = require('Socket io')(server);
io.on('connection', function(socket){
console.log('user connected with socketId '+socket.id);
socket.on('event', function(data){
console.log('event fired');
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server.listen(3000);click below button to copy the code. By Socket tutorial team
Setup with Express
- Express app can be passed to http server which will be attached to Socket io.
var app = require('express')(); //express app
var server = require('http').createServer(app); //passed to http server
var io = require('Socket io')(server); //http server passed to Socket io
io.on('connection', function(){
console.log('user connected with socketId '+socket.id);
socket.on('event', function(data){
console.log('event fired');
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
server.listen(3000);click below button to copy the code. By Socket tutorial team
Client Side Setup
- Check the Hello World example above for the client side implementation.