Chirantan Banerjee
Chirantan Banerjee

Reputation: 1

Difference in io.on and socket.on in socket.io

Is this diagram gives correct representation of io and socket in node js server? What I want to say is .... First we write- const io=require("socket.io")(8080);

then we write- io.on('connection',(socket)=>{//some logic...}) Is this establishes some kind of socket.io server running inside node js server at port 8080? after this inside io.on we write- socket.on(event,action); Is this socket.on like opening in socket server where users get connected??? If the diagram is a mistake plz correct me.. THANK YOU ... Diagram Here

Upvotes: 0

Views: 229

Answers (1)

Jake
Jake

Reputation: 199

io.on listens for all events, which allows you to do this:

const io = require('socket.io')(8080);

let users = 0;
io.on('connection',(socket)=>{
  users++;
  console.log(users);
});
io.on('disconnect',(socket)=>{
  console.log('A user disconnected!');
});

On the other hand, socket.on only listens for data on it's own server, so it will ignore other servers' data.

Upvotes: 0

Related Questions