
GAE + GWT 005 – Channels
June 12, 2011I stumbled across the Channels service provided by the AppEngine and thought to myself, this is exactly what I need and should have been using for my collaba-doodle! Basically a Channel lets you have a direct socket connection between server and client where the server can immediately ‘push’ messages down the socket and to the client. Since the socket connection remains connected (for a maximum of 2 hours, or until closed by the client) it makes it much more convenient to synchronise data amongst collaborators rather than polling the server each time with a RPC.
So I decided to give it a try, but in order to not break my existing collaba-doodle, I made a shoutbox. If anyone has noticed, I’ve always had the shoutbox on my showcase but it was only a place to vent your spleen into the void of nothing-ness. So to make it useful, I added the channel service to create a real shoutbox which broadcasts to other users.
When the module loads up, I give the user a random ID (it’s not perfectly unique as I’m only using a simple number generator and not checking for collisions) – as we’re only testing proof of concepts with this showcase, not devloping perfect applications. So with that ID, we send it to the server to generate a ‘token’ for the user which acts as the unique socket connection that will be used by the server to communicate with the client. The server logs the ID so it knows to send future messages to them.
I set up the channel on the client side using the gwt-gae-channel library as it makes life much easier than having to fiddle with javascript. Once the connection has been established, the user is free to send messages. When a message is sent, an RPC is invoked to the server with the sender’s ID and the message. The server then looks at its table of established connections and passes the message onto all connected clients. Done. A simple shoutbox (or chatroom, which ever you want to call it).
Now that is done, there’s definately things which can be added like ‘rooms’ as chatting with +100 people would be chaotic [albeit fun]. But after this simple experiment with channels, I can try to incorporate the ideas to my Collaba-Doodle =)
It’s on the showcase now to play around with: ajt HYPHEN showcase DOT appspot DOT com