Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Allow sharing sockets between processes #2323

Closed
VanCoding opened this issue Dec 13, 2011 · 13 comments
Closed

Allow sharing sockets between processes #2323

VanCoding opened this issue Dec 13, 2011 · 13 comments

Comments

@VanCoding
Copy link

Hi guys!

It would be GREAT to be able to share sockets between node.js processes. So, a server node could accept incoming connections and pass them to other nodes, which are then absolutely independent of the server node. They are not forks or childprocesses, like with the "cluster" solution.

The process that gets the socket could now directly write & read to the socket. No piping would be needed.
A socket could be passed between processes as many times as needed.

Example for a game server concept:

  1. A login server accepts a the connections & the client logs in over it
  2. After the login, the server passes the socket for example to one of the "game-lobby" processes
  3. The user joins a game. When the game starts, the lobby server passes the connection to a game process, which runs only for this game.
  4. After the game ends, the game process passes the socket back to the lobby process, which updates his statistics.

Using piping, this would be impossible, since it would be to slow. Being able to read/write to the socket DIRECTLY, heavily decreases the latency compared to classic piping.

Not only for this game server concept, this would be SO USEFUL to all sorts of applications. I bet that programmers would love it.

Hope you implement it :)

@VanCoding
Copy link
Author

No comments on this feature request?
It is possible!

http://tangentsoft.net/wskfaq/articles/passing-sockets.html

we would just need 2 functions in the net module:
net.getSocketID(Socket) : string
net.getSocket(SocketID); Socket

This would be an absolute killer feature!

@AndreasMadsen
Copy link
Member

This is now supported by `child_process.fork().send(message, handle);

So please close.

@VanCoding
Copy link
Author

Where can I read about how it works? Is this feature documented anywhere? Which version do I have to use?
`child_process.fork().send(message,handle); doesn´t look like it would be an implementation of the feature I requested..

@AndreasMadsen
Copy link
Member

Oh, I might have missread.

The API is documented here: http://nodejs.org/docs/latest/api/child_processes.html#child_process.fork

@VanCoding
Copy link
Author

Yes, I think so.
This function only allows a fork-process to listen on the same port like his master.
Reading my feature request again, will maybe make clear what I want :)

@VanCoding
Copy link
Author

I´ve an idea how an implementation into the current cluster logic could look like:

//sampleServer.js
var cluster = require("cluster");
var net = require("net");

if(cluster.isMaster){

    var workerA = cluster.fork();
    var workerB = cluster.fork();

    workerA.send("A");
    workerB.send("B");

    net.createServer(function(c){
        c.write("what worker do you belong to?");
        c.on("data",function(d){
            c.delegate(d == "A"?workerA.pid:workerB.pid);
        });
    }).listen(80);

}else{
    process.on("message",function(m){
        if(m == "A"){
            net.createServer(function(c){
                c.write("you are now connected to worker A. What can I do for you?");
            }).listen(80);
        }else if(m == "B"){
            net.createServer(function(c){
                c.write("you are now connected to worker B. What can I do for you?");
            }).listen(80);
        }
    });
}

//sampleClient.js
var net = require("net");
var c = net.connect(80,function(){
    c.once("data",function(d){
        if(d == "what worker do you belong to?"){
            c.write("A");
            c.on("data",function(d){
                console.log(d+"");
            });
        }
    });
});



//output
[client connects to server]
Server (master): what worker do you belong to?
Client: A
Server (worker A): you are now connected to worker A. What can I do for you?

@isaacs
Copy link

isaacs commented Feb 6, 2012

We're not going to do this at this time. This is a cute API, and surely handy, but doing it this way would require massive change to how it works, and could probably never be as performant as what we've got now.

@AndreasMadsen
Copy link
Member

@VanCoding I have made a pull request about this, but there is still issues there need to be solved.

@VanCoding
Copy link
Author

omg it's so great so see someone has implemented such a feature. This definately must be pulled!
The possibilities this features offers are awesome! Thank you so much for your work!

@VanCoding
Copy link
Author

I've created a module for this now. It doesn't work on windows tough.

https://github.com/VanCoding/node-ancillary

@coolaj86
Copy link

Did this functionality ever make it into mainline?

@cjihrig
Copy link

cjihrig commented Sep 27, 2016

Yes, sockets and servers can be sent between processes. See https://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle_options_callback.

@catalin-enache
Copy link

catalin-enache commented Feb 4, 2017

The documentation says that its only possible between 2 nodejs processes having parent-child relationship
(which does not provide too much usage freedom).
Sending fd between unrelated processes is not possible - or at leat not documented.
To be able to send/receive a fd, the socket should be allowed to read/write ancillary data.
Ancillary data is different than usual socket message passing.
The unix socket makes this feature possible (its an OS feature), only it is not exposed in nodejs.
In python for example this is possible using plain socket communication
by sending/reading to/from unix socket the ancillary data:
https://docs.python.org/3/library/socket.html#socket.socket.recvmsg
It would be awesome for example to be able to recieve fd into nodejs from a python/ruby/c/whatever process
and send fd from nodejs to a python/ruby/c/whatever process as these languages seems to already
provide support for it.
One use case would be: having nodejs receiving a game-client connection,
launching its game-server written in C or whatever language, forwarding the socket
while also being able to intercept (if needed) the bytes exchanged between game-client and game-server.
Its just a unix socket communication protocol that needs yet to be exposed in nodejs too.
Not having this feature buit in prevents the scenario where nodejs cold be used as a game-server controller.
Relying on 3rd party native nodejs extension could be a solution but on one hand
the libary may or may not be maintained and on the other hand this feature too essential to not be a part of nodejs core.
If I may kindly ask, please reopen this feature request and make it into the core.

Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants