2

I'm trying to create a dynamically created room for my websocket server. My file server needs to tell the client info about the file they uploaded, like processing progress and stuff like that. This is my view:

export function myView(req, res) {

  let file = req.file;
  let socketpath = ('/sockets/' + socketid + '/') // A randomly generated id
  let wss = new WebSocketServer({port: 3030, path: socketpath});

  ...
}

This works but only once. As soon as I try sending another file to the server, I get this error:

Error: listen EADDRINUSE 0.0.0.0:3030
    at Object.exports._errnoException (util.js:873:11)
    at exports._exceptionWithHostPort (util.js:896:20)
    at Server._listen2 (net.js:1250:14)
    at listen (net.js:1286:10)
    at net.js:1395:9
    at nextTickCallbackWith3Args (node.js:453:9)
    at process._tickDomainCallback (node.js:400:17)

Obviously this is because I'm creating another wss at the same port. But I need a room for each client session upload, how do I make that?

Edit: I did google this, but didn't find anything.

5
  • What is a dynamic route and why do you think you need it? You need to describe the actual problem you're trying to solve. You don't create a separate webSocket server for every client. You can create one server that all clients connect to and usually the clients identify themselves (who they are) so the one server can serve all their needs. If you were going to create multiple servers, then they all have to be on different ports and you'd probably have to use CORS so the client could reach them. Commented Sep 25, 2016 at 15:07
  • @jfriend00 Sorry for the confusion, I'm kinda new to websockets. By routes I mean like a "room" that a client can connect to. So for example: websockets.com/someroom or websockets.com/someroom2 Commented Sep 25, 2016 at 15:09
  • Have the client connect to your one webSocket server and then send a follow-on message about what room they want to be in. And, please EDIT your question to describe this rather than have it only be in the comments. Or, use a query parameter to specify the room upon initial connection. Commented Sep 25, 2016 at 15:09
  • FYI, if you use socket.io (a helpful layer on top of webSockets), it has both namespaces you can connect to and rooms you can join built right in. Commented Sep 25, 2016 at 15:11
  • I've edited my question, hopefully making it bit more understandable. Thank you. Commented Sep 25, 2016 at 15:18

1 Answer 1

4

Usually, you would create only one server and all clients would connect to that one server. The client could specify a room they wanted to connect to either by using a query parameter on the connection URL or by sending a message to the server after connection specifying which room they wanted to join. You would then keep track of which sockets where in which room on your one server so your server could implement whatever features you want with those room (such as send a message to every one in room "xxx" or send message to all the other users in room "xxx").

The only way to create multiple servers would be have each one be on its own port, but then the client would have to know which port to connect to and you'd have to implement CORS so the browser would allow the connection. A unique server for each separate connection gets messy real fast and is generally not required.

Sign up to request clarification or add additional context in comments.

Comments

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.