How to organize an app on socket.io, node.js

I'm going to write an application in node.js using socket.io. All the examples that I have seen on the Internet are quite simple, they all fit in one file and the application has no structure. I want to write a more complex application more correctly and I have problems with this.

I found on github one option for how to put all the logic associated with sockets in a separate file.

In the app file.js a web server is being created. Then the next line of code:

io.sockets.on('connection', Mysocket);

Mysocket is a self-made module that is connected above. It contains the following code:

module.exports = function (socket) {
 socket.on('test', (data) => {
    io.to(socket.id).emit('test_1');
 });
}

In this approach, everything works except sending a message to the client. An error appears in the Mysocket file stating that io is undefined. If you replace io with socket (in general, I do not understand what the difference is), the error does not appear, but the message is not sent.

There were still thoughts to create a separate module with server creation, initialization socket.io and connect it to the app as well.js and to Mysocket.js, but I doubt that this is an adequate solution, I have not seen this anywhere.

In general, the question is: how to properly organize an application using socket.io large enough to hold everything in one file.

1 answers

Write a separate module for creating an object socket.io just a very adequate solution. It can describe the connection, add listeners to the basic (system) events, implement the authentication mechanism and basic error handling, add the ability to connect via HTTPS, etc.

Listeners of custom events (your ones that implement business logic) can be grouped by controllers as separate methods. You can link them all in a separate module / class - the router. But already in the main file, call the creation of a socket instance, registration of all routes, the db connection module, etc.

About the example and the io and socket objects. The difference between them is that the io variable is usually assigned an instance socket.io, and socket is the object of a specific connection. I think the example will be clearer

io.on('connect', socket => {
    socket.on('hi', data => 
        console.log(data);
        // Отправка сообщения конкретному сокету внутри колбека полдключения
        socket.emit('hi-reply', {message: 'hi'});
    );
});

If you need to send a message to a specific socket outside of the connection callback, you need to send a message to the room. To do this is via the global io object:

io.to(socket.id).emit('test_1');

In your code example, this would be equivalent to socket. emit ('test_1'), since the call goes inside the connection callback.

Https://socket.io/docs/server-api/ - in the official documentation, to understand what was named in the io example, read the server and namespace sections. For socket - respectively, socket

 0
Author: Roman Lytvynov, 2020-06-06 13:07:50