Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Lesson 1: Communicating by using WebSocket CHAPTER 10 423


Quick check
■■Do the WebSocket protocol and WebSocket API provide native support for load
balancing in a web farm?

Quick check answer
■■No, there is no native support for web farm load balancing with WebSocket
protocol and the WebSocket object.

Using WebSocket libraries


Dealing with timeouts, dropped connections, incompatible browsers, and web farms can be
a daunting task. Wouldn’t it be much better to have a library that can handle this task for
you in a consistent manner? It turns out that there are libraries to help you, but you need
to understand that these are client and server libraries; you use the library at the client and
server. The following is a short list of libraries that can help solve some of these problems.
Because WebSocket is not yet a recommendation, and these are relatively new products, this
list should get you started, but do some web searching for the latest releases of these and
other products.

SignalR
SignalR is a Microsoft library for ASP.NET developers. It is meant to be implemented on an
ASP.NET website, using a .NET language such as C# or Visual Basic. The client code is still
JavaScript, however.
SignalR simplifies the addition of real-time web functionality to your applications. It uses
WebSocket when it’s available but gracefully falls back to techniques such as long polling
when WebSocket is not available. The best part is that you don’t need to change your code
for this transition. You might have clients that support WebSocket and clients that don’t; the
clients work well together even if they don’t support WebSocket.
SignalR also provides connection events to which you can subscribe for connection
management. SignalR is so easy to use that you should consider this if you are working on an
ASP.NET website. SignalR can also work on the Windows Azure cloud. You must provide state
management for SignalR to share data between servers, but it is doable.

Socket.IO
Socket.IO is a WebSocket library created by Guillermo Rauch, CTO and cofounder of
LearnBoost; developer and inventor of Socket.IO, Engine.IO, and codestre.am; JSConf.ar cura-
tor; open-source enthusiast; and blogger.
Free download pdf