简体   繁体   中英

node.js pass socket.io socket to forked process

I'm wondering if there is any way to pass a socket.io socket to a forked process in node.js, or reconstruct a socket.io socket in the forked process?

child = require("child_process").fork("worker.js")
io.sockets.on("connection", function(socket){
    child.send("socket", socket); //this definitely fails now
})

Solution : Use Redis

Im sure you will be happy to know about the existance of socket.io-redis

With it you will be able to send/receive events (share clients) between multiple instances of your code (processes or servers). Read more : socket.io-redis (Github Repo.)

With this you can scale :

  1. vertically (increase the number of threads per machine)
  2. horizontally (deploy more "masters" accross more machines)

There is an example app at github

This is a very simple Node.JS client/server application that shows how to setup Express.js, Socket.IO 1.x and socket.io-redis together.

This is very useful as a starting point to understand how to scale Socket.IO RealTime Applications that need multiple server processes. Redis is used as a storage point (adapter) in order to run multiple socket.io instances in different processes or servers that can all broadcast and emit events to and from each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM