1

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);
    });
});
2
  • It's not very clear what "implement the child process with socket io" means here. Please show relevant code and explain the problems you ran into and your attempted solution in more detail. Also, if you show the code for what is taking all that CPU, folks here may be able to offer different/better solutions to your CPU usage problem than what you've tried so far. Commented Jul 13, 2021 at 14:54
  • This code looks incomplete. Inside the socket.on('connection' ...) handler you have references to res and req, 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. Commented Jul 13, 2021 at 16:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.