1

I have a nice little WebSocket app using http-kit server, and I'm feeling pretty good about myself. Now I want to add different "rooms" (the list of which should be dynamic) to my app, but I am having difficulty finding any documentation or example projects. I'm not afraid to spin my own solution, but it's nice to lean on others' experiences. Does anyone know of any examples of a similar implementation?

I can think of two approaches:

1) I could just keep the "room" in state along with the channel, then just send! to the channels associated to that room. Seems like the easiest approach, but then I'm filtering through each attached channel every time I broadcast a message.

2) I could build a new socket endpoint every time a new room is opened, and send the new URL back to the front end (or send the existing URL if the room was already opened), which would then drop the old socket and open a new one to the new url. Some overhead in building the new endpoint, but then I can just broadcast to every channel subscribed to it.

Any other ideas or input? I'm still pretty new to programming with WebSockets and with Clojure, so I get the feeling there may be a better way.

TrivialCase
  • 1,060
  • 2
  • 14
  • 27

1 Answers1

2

Both of your solutions are completely fine, though #1 would be slightly improved by maintaining an aditional map in the state so you would have

  • a map from chan --> room
  • antoher map from room --> vector of chans.
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284