node-cluster-socket.io icon indicating copy to clipboard operation
node-cluster-socket.io copied to clipboard

Using Express.js instead of Net

Open MickL opened this issue 6 years ago • 9 comments

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 avatar Nov 21 '17 12:11 MickL

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

MickL avatar Dec 01 '17 13:12 MickL

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.

danecek099 avatar Jan 17 '18 12:01 danecek099

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

fatfatson avatar Feb 08 '18 13:02 fatfatson

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.

MickL avatar Feb 08 '18 13:02 MickL

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

fatfatson avatar Feb 09 '18 04:02 fatfatson

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.

MickL avatar Feb 09 '18 10:02 MickL

@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.

fatfatson avatar Feb 09 '18 12:02 fatfatson

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

MickL avatar Feb 09 '18 13:02 MickL

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);
		});
	});

fatfatson avatar Feb 10 '18 01:02 fatfatson