Take the 2-minute tour ×

I'm currently using Jetty as my websocket server and I'm trying to make a real-time game. I'm really new to this. So far I've managed to work on the easiest one (which is the chat). Now, I have a couple of issues:

  1. I'm trying to make a list of the current users connected in the server; how do I make such feature? I don't really know if this is practical but my idea is that there's a time interval that lets the server know that "Hey, I'm still alive." I want my user list to be dynamic in such a way that if a user logs out, the list will refresh.

  2. I'm also trying to make "rooms" for my app. Basically, the people in "Room 1" can't see what people in "Room 2" are doing; the other room functionalities are already self-explanatory. I still don't have an idea for this; can someone recommend a way to implement this?

So far I've been having fun in learning this technology, and I would be very glad if there would be incoming help. These two are my issues so far. I'm also open for any suggestions for my app.

Thank you all in advance. :-)

(Uhm, can #1 and #2 be solvable solely by Javascript?)

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Your question isn't really jetty or websocket specific.

Websockets simply provide a transport mechanism for your web browser to communicate with the server - what gets set over that transport, and how the server manages information are completely separate to the websocket.

Firstly, if you haven't read it already, read this article: http://cometdaily.com/2010/03/02/is-websocket-chat-simple/

Secondly, consider using cometd to make this easier - version 2 (currently in Beta) supports websockets.

As for your questions - there are 2 things you'll need to do (cometd can help with this)

1. Define a protocol
Define a messaging protocol to sit on top of websockets. Websockets just provide a way to send chunks of data around, they don't define what that data means. You'll need to build something that does that, so you have messages that go from the client to the server like.

  • CONNECT "user"
  • JOIN_ROOM "room1"
  • SEND_MESSAGE "message"

And then you need messages form the server to the client

  • RECEIVE_MESSAGE "user" "message"

You need to come up with some reliable way to send that data over a websocket. You can invent your own simple protocol (like the Jetty chat example does), you could use JSON, or XML, or CometD channels.

2. Server-Side State
If you've got simple chat working, then you must have made a start on this, but the other bit you need is a way to keep track of

  • how many users are connected
  • what their names are
  • which socket they're connected to
  • which room they're in

Depending on how complex your system is going to become, you might be able to just store that in 1 big singleton on the server.

share|improve this answer
Hi Tim, thanks for your reply! How do I know which users have been disconnected? –  ton Mar 8 '11 at 11:13
add comment

Your Answer


