A
A
Alexey2018-07-24 21:21:14
Node.js
Alexey, 2018-07-24 21:21:14

SocketIO and a large number of events?

The question arose, in the vast majority of socket examples, the following construction is used:

io = require('socket.io').listen(httpsServer);
io.on('connection', function(socket) {
  socket.on('listener1', function(e) {
    //code
  }),
 socket.on('listener2', function(e) {
    //code
  }),
  ...etc
});

The question is, is it correct to write code like this? After all, when connecting 100 people, the node will create N * 100 wiretapping events. How to get out of such a situation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abcdefgk, 2018-07-25
@Abcdefgk

Yes, there is no "situation". When 100 clients connect, 100 socket objects will be created - each with a unique ID. And "listeners" of events are not asked, they only listen to them and, upon receipt, give them to handlers - this is an abstraction common to all connections.

D
de1m, 2018-07-24
@de1m

In my opinion the example somehow looks strange.
"socket.on" occurs on some event, not on the arrival of a new user.
That is, for example, a client (one or a thousand) says A on the server side

io = require('socket.io').listen(httpsServer);
io.on('connection', function(socket) {
  socket.on('getUserInfo', function(e) {
    //code
     ...
    socket.emit('sendUserInfo', data)
  })
});

That is, it does not depend on the number of clients. Though yes, open TCP connections with each client will hang.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question