L
L
LemanRass092017-02-01 21:37:26
Algorithms
LemanRass09, 2017-02-01 21:37:26

How to correctly build an algorithm for interactions between modules in Node.js?

Hello.
I work in WebStorm.
I am making a website using the express package and socket.io.
This is how the basic configuration of an ordinary blank page of my site looks like:

var express = require('express');
var network = require('../lib/socket.js');
var users = require('../lib/users.js');
var log = require('../lib/log.js');
var router = express.Router();

router.get('/', function(req, res, next) {
    if(req.user !== undefined) {
        var token = req.cookies.token;
        log.Debug("[LawsPage] Connected token: {0} ", [token]);
        users.getDBUserByToken(token, function (user) {
            if(user === undefined) {
                res.redirect('/');
            } else {
                res.render('laws', {
                    username: user.name,
                    useravatar: user.avatar,
                    userbalance: user.balance
                });
            }
        });
    } else {
        res.redirect('/');
    }
});

network.io.on('connection', function (socket) {
    var token = socket.handshake.query.token;
    users.onConnectSocket(socket, token);
    log.Debug("[Laws -> Socket.IO] New user connected! Token: {0} Socket ID: {1}", [token, socket.id]);

    socket.on('disconnect', function() {
        users.onDisconnectSocket(socket);
        log.Debug("[Socket.IO] User disconnected! Token: {0} Socket ID: {1}", [token, socket.id]);
    });
});

module.exports = router;

The problem is that the more pages on the site, the more times the socket.io on connection event is defined. And then when some user sends any event via socket.io - it is executed as many times as there are pages on the site that have on connect callback, even if the socket.io listener is initialized in a hotel script that is executed exactly exactly 1 time and looks like this:
var io = require('socket.io');

//Инициализация экспрессом
module.exports = function(server) {
    var listener = io.listen(server);
    console.log("[Socket.IO] Listening...");
    module.exports.io = listener;
};

This of course suggests that you need to transfer the connection callback to this socket.io initialization script as well. I did this today, but ran into another problem:
All the functionality of the page remained in the page script, and of course, when the user sends a packet, he comes to the script where socket.io is initialized, then he must access the page script where the user is located and the necessary functionality. I did it with require. I just connected the page to the socket.io initialization script and that's it, now I can safely use the functionality of different pages at the request of the user from the socket.io initialization script. But when the page itself wants to send a packet to the user, without his request, I need to connect the socket.io initialization page to the script of the site page in order to get access to the io object and, accordingly, the emit function. But this is apparently called a ring dependency and you can’t do it that way.
How can I be in this situation? How can I properly build the module tree?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2017-02-01
@vshvydky

redux?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question