Skip to content
← Back to Community
Multiple Websocket Servers
Profile icon
Crosis

I was wondering if there was a way to create new websocket servers using the same repl as I was trying to create private rooms and thought that a separate websocket server may solve this. Does anyone know if this would work?

Code here

@19wintersp
sorry but I need your help

@Coder100
pls help meh

@GatewayDuckYT
get on le chat

Voters
Profile icon
Baconman321
Profile icon
dudeactualdev
Profile icon
Crosis
Comments
hotnewtop
Profile icon
19wintersp

This is quite simple: you need to create the illusion. One way of doing this would be to convert from requesting wss://example.com/ to wss://example.com/?room=abcdef. On the backend, I assume you're handling this by using a list (or object) of connections:

const connections = []; wsServer.on("connection", (sock, req) => { connections.push(sock); /* ... */ });

You will need to convert that (appropriately, don't copy-paste) to this:

const url = require("url"); const connections = {}; wsServer.on("connection", (sock, req) => { const room = url.parse(req.url, true).query.room || "default"; //Find the "room" query parameter if (connections[room] == undefined) connections[room] = []; //Set up the list of connections if it's not there already connections[room].push(sock); //Add this connection to the room /* ... */ });
Profile icon
19wintersp

And no, you can't [have multiple WebSocket servers on one repl].

Profile icon
Crosis

@19wintersp
This did not work for me, am I doing anything wrong?

Profile icon
Crosis

@19wintersp
For this whenever I am for example sending a message,
would I do

connections[room].forEach(({ socket }) => socket.send(`m${name}`));

I already have the code you put above.

Profile icon
19wintersp

@Crosis
Looks good

Profile icon
Crosis

@19wintersp
When I did this, it provided an error of "Cannot read property send of undefined" Edit: I think I have resolved the problem, waiting for someone else on chat to test. Also, for your code above I had to change the req value in const room to req.toString(); Could that be affecting it?

Profile icon
19wintersp

@Crosis
I see the issue, change to this:

connections[room].forEach(socket => socket.send(`m${name}`));
Profile icon
Crosis

@19wintersp
Ok, I will try that

Profile icon
Crosis

@19wintersp
It gave an error of "socket is not a function at socket.send"

Profile icon
19wintersp

@Crosis
No, it didn't. Read it carefully.

Profile icon
Crosis

@19wintersp
socket.send is not a function that is what the error said srry

Profile icon
Crosis

@19wintersp
what should I do?

Profile icon
19wintersp

@Crosis
What line is this on?

Profile icon
Crosis

@19wintersp
line 400, would you like me to ask ryan to add you back to repl?

Profile icon
19wintersp

@Crosis
If the other sends work, revert back to your original:

connections[room].forEach(({ socket }) => socket.send(`m${name}`));

And no, you shouldn't toString req. That was my mistake, I've updated my post.

Profile icon
Crosis

@19wintersp
I will try that

Profile icon
Crosis

@19wintersp
It still says "cannot read property send of undefined"

Profile icon
19wintersp

@Crosis
Can you change it to:

connections[room].forEach(d=>console.dir(d));
Profile icon
Crosis

@19wintersp
what is the d=> and everything for, and how would I substitute it?

Profile icon
Crosis

@19wintersp
Would you like me to ask ryan to add you back to the repl?

Profile icon
19wintersp

@Crosis
Fine

Profile icon
Crosis

@19wintersp
you were added

Profile icon
Baconman321

Please don't ping multiple people. Yeah, tussiez and I had the same problem when we are going to make multiple rooms for multiplayer sortacraft. This would require dynamic rooms and would work best using nodejs or another server-side language. In my mind i have the idea of creating a "fake" url and handling that using the serve functions in golang/nodejs. It will dynamically store session data for multiplayer and be able to handle the information differently based of of what URL of the data being sent. Right now it's a very rough idea and will probably be changed a lot during actual build, but I think I can achieve this through different threads in golang. Otherwise, I just make sure to dynamically add url handlers in nodejs/golang and intercept data and handle it and send it back to the room. It's going to be very hard, though.