I am trying to create rooms with 5 members they are sharing the data between each and everyone with each 100ms frequently. when I deploy this logic with 20 rooms the CPU utilization goes from 70% to 80%. So I decided to run with this child process but I can't able to implement the child process with socket io. if anyone has an idea please help with this. Thanks
Client Script
socket = io("ws://localhost:52300", { transports: ["websocket"], reconnection: false });
socket.emit('joinRoom',{roomId:botsetId,player:player_id});
socket.on('updatePosition', function (data) {
console.log(data);
});
function updateData()
{
socket.emit('updatePosition', { "p": { "x": 0.5, "y": 0.357, "z": args.z }, "l": 20, "s": -0.01578048, "m": 6.809624, "a": 1, "c": 44.39658, "username": player_id,"roomId":socketRef[player_id].roomId });
}
Server Script
var multiplayer_server_port = (process.env.PORT || 52300)
var socket = require('socket.io')(multiplayer_server_port)
var app = require('express')();
var bodyParser = require('body-parser');
const { disconnect } = require('process');
var http = require('http').Server(app);
var port = 54500;
const {fork} = require("child_process")
socket.on('connection',function(client){
console.log('########-Making Connection With Client-########');
const childProcess = fork('./updatePosition.js');
childProcess.send({"number": parseInt(req.query.number)})
childProcess.on("message", message => res.send(message))
client.on('joinRoom',function(data){
var roomId = 'SET_'+data.roomId;
client.join(roomId);
console.log('Player '+data.player+' Joined the room '+data.roomId);
console.log(socket.sockets.adapter.rooms);
});
client.on('updatePosition',function(data){
console.log(data);
//client.broadcast.to('updatePosition',data);
var roomId = 'SET_'+data.roomId;
client.broadcast.to(roomId).emit('updatePosition',data);
});
});
socket.on('connection' ...)handler you have references toresandreq, but those are not shown anywhere. This implies these are inside of some http request handler which would be a problem. Also, why are you creating a separate child process for every single socket.io connection? In the future, when you update your question in response to a comment question, please comment back to that person. Otherwise, they will never know you did anything.