This is a rather technical description of a technology created by us at Lusion Technologies. While it may eventually be usable by any website owner it is only intended for developers at the moment. If you are interested in a simple demonstration of the server, you can take a look at http://www.lusion.co.za/labs/speak/
Creating a javascript chat script has become a trivial task with the latest JavaScript frameworks – but the idea of a PHP & MySQL setup to deal with the users is excessively wasteful. Most chat systems use a polling system where it checks if there are any new messages every few seconds… with a lot of users it can quickly add up to many hundreds of connections per second and when each connection has to pass through Apache to PHP before it is processed it really adds up fast. Keeping connections open help out enormously but when everything is still passing through Apache and each connection with its own PHP process which in turn stores everything in a database – there had to be a better solution.
Speak is an efficient message server written to solve the problem of slow online chat back-ends. It uses a back to basics method of public channels which every application using the message server uses. Messages can be written to a channel with a simple HTTP call to URL such as http://speak.lusion.co.za/write?channel=test&message=howdy, and can be read using a URL such as http://speak.lusion.co.za/read?channel=test&from_id=1. Since there is no way of listing the current channels, private channels can create just by using a secret name.
Firewalls, Transparent Proxies and Cross-Domain JavaScript
As Speak is a dynamic application running over the HTTP protocol, transparent proxy servers running on clients machines can often cause a problem. While making sure every request has a unique url will solve most of the problems, the one key transparent proxy problem I encountered while developing Speak was the fact that transparent proxies do not always terminate the connection when the client terminates their connection. While this isn’t a major problem Speak functions better if it is avoided – for that reason the Speak server also runs on port 7732.
While Speak works better on its alternate port 7732, it cannot be used by default as many firewalls will simply block the connections. In order to use both, I use a test to determine if the servers can be accessed. Loading the image http://speak.lusion.co.za/image-test through JavaScript can be used to easily check if the port is open (img.complete == true).
As Speak runs on a different domain the main website you’ll encounter an error when trying to load the pages through Ajax due to cross-domain security tests. There are a couple ways to bypass this problem (summary of the methods on snook.ca). The method I use in my demonstration is dynamic script tags. This method calls the Speak URL with dynamic JavaScript tag, which runs a function with the JSON code. For an example, to write to the test channel, and call the javascript callback “runme”, you would include the script tag <script type=”text/javascript” src=”http://speak.lusion.co.za/write?callback=runme&channel=test&message=howdy“></script>
Introductions
Speak is designed to function as a Chat server as well as just a message server. To act as a chat server, the critical feature is the ability to be able to track who is in the chat room at the time. Speak achieves this through the use of “introductions”.
When you join a channel, you can introduce yourself with a message using the ‘introduce’ call (http://speak.lusion.co.za/introduce?channel=test&message=Josh). This will assign you a connection id and give you a secure auth code to prove its your connection. You can then send messages as this connection by tagging the details on as parameters (i.e. http://speak.lusion.co.za/write?channel=test&message=howdy&connection=1&auth=PIVR02uC). If you’re trying to introduce yourself by hand you better be fast – the connection times out after 5 seconds (this is one of the numbers that may change in the future).
Once you introduce yourself, everyone currently in the chat room will receive your message with type “introduction” and all of your messages will include a ‘from’ variable with your connection id. Anyone new to the channel will receive all the introductions if they read with from_id=0. If your connection times out, or if any of your connections are closed by you, your connection is closed and a message with type “goodbye” is added to the channel. If the introductions include the user names, this can be used to populate a user list.
Private messages
While I have not yet created any demonstration of private messages (and any proper implementation needs global connection authorization – a feature that will be added to Speak in the near future). Private messages work by using paired read and write channels (one channel can only read the messages written to a channel that can not be read from). Creating these paired channels can be done with the “create” command: http://speak.lusion.co.za/create
These channels do not yet have support for Introductions, and their full implementation has not yet been decided.
Using Speak
I have developed Speak primarily for my own needs but as the server is public (and this post explains in depth how to use the server) – its open for now but with no guarentee. I’d also be interested to see how much load the server can take (it is by far the most efficient JavaScript chat solution I have seen yet). Updates to Speak will be posted on this blog, and if you have any comments (or wish to discuss a permanent solution) just leave a comment on this post.
A working demo of a chat room using MooTools and Speak is available at: http://www.lusion.co.za/labs/speak/
Click here for a downloadable demonstration



