title: How to create socket io rooms with node js child process
body: <p>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</p>
  <p>Client Script</p>
  <pre><code>socket = io(&quot;ws://localhost:52300&quot;, { transports: [&quot;websocket&quot;], reconnection: false });
  socket.emit('joinRoom',{roomId:botsetId,player:player_id});
  
  socket.on('updatePosition', function (data) {
          console.log(data);
      });
  
  function updateData()
  {
  socket.emit('updatePosition', { &quot;p&quot;: { &quot;x&quot;: 0.5, &quot;y&quot;: 0.357, &quot;z&quot;: args.z }, &quot;l&quot;: 20, &quot;s&quot;: -0.01578048, &quot;m&quot;: 6.809624, &quot;a&quot;: 1, &quot;c&quot;: 44.39658, &quot;username&quot;: player_id,&quot;roomId&quot;:socketRef[player_id].roomId });
  }
  </code></pre>
  <p>Server Script</p>
  <pre><code>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(&quot;child_process&quot;) 
  
  socket.on('connection',function(client){
      
      console.log('########-Making Connection With Client-########');
  
      const childProcess = fork('./updatePosition.js');
      childProcess.send({&quot;number&quot;: parseInt(req.query.number)})
      childProcess.on(&quot;message&quot;, message =&gt; 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);
      });
  });
  </code></pre>