Andrew
Andrew

Reputation: 238717

How to keep track of users (connections) in a WebSocket Chat application

I'm using the em-websocket (Ruby EventMachine) gem to serve up a WebSocket chat application. I was able to get a demo app working pretty easy (based on this gist). Now I would like to go a little more advanced. Currently, the only functionality is to post a message to the chatroom, but it is not show who sent the message (because I don't know how to keep track of this information).

I'm assuming the way to do this is to send JSON back and forth with some sort of ID, but where does this ID come from? Is there some sort of reusable ID that is unique to each connection that identifies the user?

Sorry if this is a dumb question, I am new to working with WebSockets.

Upvotes: 3

Views: 4688

Answers (1)

David Grayson
David Grayson

Reputation: 87406

I have written a basic chat app using em-websocket. You can see the code here:

https://github.com/DavidEGrayson/websocket-chat/blob/master/rws1/server.rb

The em-websocket gem creates a web socket object to represent each connection. I made my own Client class which holds a user name and a reference to the websocket object.

Feel free to ask me questions about that code.

Upvotes: 4

Related Questions