0

In my Node app.js, I don't necessary need to have my array openConnections global...but I want it in app.js and accessible by module sse_server.js for the sseStart function. How can I do this?

app.js:

var express = require('express');
var morgan = require('morgan');
var fs = require("fs");
var createDomain = require("domain").create;
var app = express();



app.use(express.static(__dirname + '/app/static/views'));
app.use(express.static(__dirname + '/app/static'));
app.use(express.static(__dirname + '/app/images'));
app.use(express.static(__dirname + '/app'));

var openConnections = [];
var sseStart = require(__dirname + '/app/scripts/sse_server.js');

app.get('/subscribe', sseStart);

var callAllApiAndSave = require('./app/scripts/api_scripts/call_all_api.js');
var db = require(__dirname + '/app/data/db.js');

var mainDomain = new createDomain();

mainDomain.run(function () {
    mainDomain.on('error', function() {
        console.log('yoyoyoyoyoyoyoyoyoyoyoyoyoyoyo');
    });
    mainDomain.on('error', function(er) {
        console.log('error, but oh well', er.message);
    });
    db.connectDatabase();
    setInterval(callAllApiAndSave, 120000);
    var server = app.listen(9000, function() {
            console.log('Listening on port %d', server.address().port);
    });
});

sse_start.js:

function sseStart(req, res) {
    console.log(req);
    // set timeout as high as possible
    req.socket.setTimeout(Infinity);

    // send headers for event-stream connection
    // see spec for more information
    res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    });
    res.write('\n');

    // push this res object to our global variable
    console.log("Creating SSE connection for " + req.connection.remoteAddress);
    openConnections.push(res);
    console.log("Total current live connections: " +
                openConnections.length);

    // When the request is closed, e.g. the browser window
    // is closed. We search through the open connections
    // array and remove this connection.
    req.on("close", function() {
        console.log("Closing SSE connection for "
                    + req.connection.remoteAddress);
        openConnections = openConnections.filter(function(storedRes) {
            if (storedRes !== res) {
                return storedRes;
            }
        });
        if (!openConnections)  openConnections = [];

        console.log("Total current live connections: " +
                    openConnections.length);
    });
}

module.exports = sseStart;

1 Answer 1

2

Looking at the code you posted openConnections is only used in sse_start.js so why not put it there?

If you really want to share that array between those two files you can just put it in a separate module and require it in app.js and sse_start.js.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought about placing openConnections in sse_start.js. If I did so, I guess I would need to export openConnections as well and require it in app.js? Because my worry is that it will be removed from memory and not retain in memory.
That's not a problem you can just move it to sse_start and export it. It won't be removed from memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.