elad / node-cluster-socket.io

Writeup on how to make node.js cluster and socket.io play nice
421 stars 63 forks source link

Using Express.js instead of Net #30

Open MickL opened 6 years ago

MickL commented 6 years ago

Is it possible to use Express.js instead of Net?

I guess Express would be the default usecase for most users. Currently i do have 2 servers: Net for Socket.io (like mentioned here) and Express.js for http requests. Now i have the problem to activate SSL and Cors on Net while it was pretty easy on Express.js.

MickL commented 6 years ago

Is "net" even capable of using http long-polling? The node.js documentation says net.createServer() creates a TCP server but no http server?

danecek099 commented 6 years ago

Same question here. How can you get connection out of Express? I would like to have smth like first page on the main script and then pass it to the worker i choose on the page... When i use Express instead of net on the first connect, a can´t end the connection to the first Express and pass it forward.

fatfatson commented 6 years ago

yeah, this example doesn't use socket.io at all, it just shows how to move tcp connection.

MickL commented 6 years ago

Actually SSL needs to be handled by NGINX before it gets to Net and then Socket.io. Works fine for me.

Im still curious if its possible to use only Express.

fatfatson commented 6 years ago

@MickL how could you restore a tcpnet connection handle to a socket.io instance?

MickL commented 6 years ago

What do you mean by restore? Net just forwards the TCP connection to the child process and socket.io picks it up and has a direct tcp connection to the user then without Net involved.

fatfatson commented 6 years ago

@MickL for restore I mean just the same as you say

socket.io picks it up

how to pick the raw tcp connection up as a socket.io's client socket?? we can emit and receive event at the later, but not the low-level connection.

MickL commented 6 years ago

Im pretty sure the code here just forwards the tcp connection and socket.io gets it as normal. What do you want to archive?

fatfatson commented 6 years ago

in this code, we get a connection from master process, but its class is net.Socket, i can't emit and recv event on it with a socket.io client

process.on('message', function(message, connection) {
        if (message !== 'sticky-session:connection') {
            return;
        }

        // Emulate a connection event on the server by emitting the
        // event with the connection the master sent us.
        server.emit('connection', connection);

        connection.resume();

        //NO EFFECT
        connection.emit('action', '---------');
        connection.on('action', function(msg) {
            console.log('recv action', msg);
        });
    });