🎉 New! Web Push Notifications for Chatkit. Learn more in our latest blog post.
Hide
Products
chatkit_full-logo

Extensible API for in-app chat

channels_full-logo

Build scalable realtime features

beams_full-logo

Programmatic push notifications

Developers

Docs

Read the docs to learn how to use our products

Tutorials

Explore our tutorials to build apps with Pusher products

Support

Reach out to our support team for help and advice

Sign in
Sign up

What are WebSockets?

An introduction

WebSockets represent a long awaited evolution in client/server web technology. They allow a long-held single TCP socket connection to be established between the client and server which allows for bi-directional, full duplex, messages to be instantly distributed with little overhead resulting in a very low latency connection.

Both theWebSocket APIand the well as native WebSocket support in browsers such as Google Chrome, Firefox, Opera and aprototype Silverlight to JavaScript bridge implementationfor Internet Explorer, there are now WebSocket library implementations inObjective-C,.NET,Ruby,Java,node.js,ActionScriptand many other languages.

A brief history of the web (from a developer’s perspective)

The Internet wasn’t built to be all that dynamic. It was conceived to be a collection of HyperText Markup Language (HTML) pages linking to one another to form a conceptual web of information. Over time the static resources increased in number and richer items, such as images, began to be part of the web fabric. Server technologies advanced allowing for dynamic server pages - pages whose content was generated based on a query.

Soon the requirement to have more dynamic web pages lead to the availability of Dynamic HyperText Markup Language (DHTML) all thanks to JavaScript (let’s pretend VBScript never existed). Over the following years we saw cross frame communication in an attempt to avoid page reloads followed byHTTP Polling within frames. Things started to get interesting with the introduction ofLiveConnect, then theforever frame technique, and finally, thanks to Microsoft, we ended up with the XMLHttpRequest object and therefore Asynchronous JavaScript and XML (AJAX). In turn, AJAX made XHR Long-Polling andXHR Streaming possible. But none of these solutions offered a truly standardised cross browser solution to realtime bi-directional communication between a server and a client.

Then along came WebSockets.

Why WebSockets are a game-changer

Finally WebSockets represent a standard for bi-directional realtime communication between servers and clients. Firstly in web browsers, but ultimately between any server and any client. The standards first approach means that as developers we can finally create functionality that works consistently across multiple platforms. Connection limitations are no longer a problem since WebSockets represent a single TCP socket connection. Cross domain communication has been considered from day one and is dealt with within the connection handshake. This means that services such as Pusher can easily use them when offering a massively scalable realtime platformthat can be used by any website, web, desktop or mobile application.

WebSockets v AJAX

WebSockets don’t make AJAX obsolete but they do supersedeComet(HTTP Long-polling/HTTP Streaming) as the solution of choice for true realtime functionality. AJAX should still be used for making short-lived web service calls, and if we eventually see a good uptake in CORS supporting web services, it will get even more useful. WebSockets should now be the go to standard for realtime functionality since they offer low latency bi-directional communication over a single connection. Even if a web browser doesn’t natively support the WebSocket object there arepolyfill fallback optionswhich all but guarantee any web browser can actually establish a WebSocket connection.