As the web moves towards being more and more realtime across platforms and applications, developers are increasingly confronted with the decision of using hosted versus self-hosted realtime services. Before the introduction of WebSockets, there were plenty of workarounds for achieving this functionality - polling for new messages by the client versus pushing messages straight from the server.
However, these two solutions resulted in unnecessary load on the server as well as timing problems, whereby a message on the server may reach the client before the client asks for it for example. This was not realtime communication in any sense of the word.
WebSockets
With WebSockets, we have a full-duplex single socket connection over which messages can be sent between client and server. This creates truly realtime communication, allowing browsers to communicate in realtime between server and client by pushing messages directly to the browser that needs the information.
There are countless libraries, startups and open source projects that make use of WebSockets, which makes it easy for developers to quickly use this technology in their projects. For example, Faye is an open source publish-subscribe messaging system that works in all major web browsers.
Faye (self-hosted) example
There are lots of greatresources out there on using Faye, but for illustrative purposes we’ll create a very simple app:
1. Install the Faye and Thin gems:
gem install faye
gem install thin
2. Create a Rackup file in your application root (faye.ru):
require ‘faye’
faye = Faye::RackAdapter.new(:mount => ‘/faye’, :timeout => 45)
run faye
3. You can now start the Faye server in your command line (it’ll run on
port 9292):
rackup faye.ru -s thin -E production
4. This server will have a Javascript file that needs to be included in
our application layout.The name of the file depends on the :mount option
provided above:
<%= javascript_include_tag :defaults,
“http://localhost:9292/faye.js” %>
5. Now we’ll create a new client - add this code to your application.js
$(function() {
var faye = new Faye.Client(‘http://localhost:9292/faye’);
});
6. Create a channel and subscribe to it in the client, also in your application.js
$(function() {
var faye = new Faye.Client(‘http://localhost:9292/faye’);\
faye.subscribe(‘/test’, function (data) {
alert(data);
});
});
7. Check if it works by opening your Rails application view in your browser. Now cURL it from the command line:
curl http://localhost:9292/faye -d ‘message={“channel”:”/test”,
“data”:”hello”}’
This should result in a notification in your browser!
Although open source solutions like Faye are relatively easy to set up, scaling real time systems can quickly become a nightmare. And there’s not very much documentation about this out there.
Pusher (hosted) example
Luckily, this is where hosted solutions like Pusher come into the picture - a hosted API that makes it incredibly easy to implement scalable realtime systems. Whether you’re developing for iOS, Android or web, Pusher and similar services have plug-and-play libraries that can get you up and running in no time.
1. Stick this Javascript in your client.
<script src=”http://js.pusher.com/2.1/pusher.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
var pusher = new Pusher(‘7c8dcbc303f793f4b4b1’);
var channel = pusher.subscribe(‘test_channel’);
channel.bind(‘my_event’, function(data) {
alert(data.message);
});</script>
2. Use cURL to check if it works!
curl -H "Content-Type: application/json" -d "hello world" "http://api.pusherapp.com/apps/1480/channels/Kg6yb3qB/eventsname=my\_event&body\_md5=5eb63bbbe01eeed093cb22bb8f5acdc3&auth\_version=1.0&auth\_key=765ec374ae0a69f4ce44&auth\_timestamp=1394461614&auth\_signature=e6d0a2fd02bbd76d6a4f3a0071ec82d7744174c060c01528d1709ee09b900513"
Scaling quickly becomes a problem for realtime systems, given that asynchronous connections are opened to potentially thousands of users atany one time. As such, hosted platforms like Pusher provide solutions to tricky technical problems so that developers can focus on what they do best.