I am writing a server for my realtime collab IDE and this is my backend, Here is the full backend index.js code
const express = require("express");
const app = express();
const http = require('http');
const {Server} = require('socket.io');
const server = http.createServer(app);
const io = new Server(server);
const userSocketMap = {};
const getAllConnectedClients = (roomId) => {
return Array.from(io.sockets.adapter.rooms.get(rommId) || []).map(
(socketId) =>{
return{
socketId,
username: userSocketMap[socketId],
};
}
) //map
};
io.on("connection", (socket) => {
//console.log(`User connected: ${socket.id}`);
socket.on("join", ({ roomId, username }) => {
userSocketMap[socket.id] = username;
socket.join(roomId);
const clients = getAllConnectedClients(roomId);
console.log(clients);
});
});
const PORT = process.nextTick.PORT || 5000;
server.listen(PORT, () => console.log('server is running'));
Here this area is constantly crashing node.js
socket.on("join", ({ roomId, username }) => {
userSocketMap[socket.id] = username;
socket.join(roomId);
const clients = getAllConnectedClients(roomId);
console.log(clients);
I tried re-importing almost everything but it didnt help
New contributor
Divyansh Pathak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.