What are WebSockets?
WebSockets are a standard protocol, built into web browsers and commonly used in Web, native mobile, and desktop application development. WebSockets allow bidirectional communication between client and server programs.
Developers use WebSockets to send data to and from a server over a relatively long-lived connection. Examples of the kinds of apps that typically use web sockets include online games, messaging programs, and data dashboards that continuously update.
Before the WebSockets was standardized, web developers generally used various kinds of HTTP polling to continuously send and receive data to and from a server. Native application developers often built their own, custom, mini-protocols. Being able to rely on the WebSockets standard is much better!
The WebSockets protocol is defined in RFC 6455, first drafted in 2008 and finalized in 2011.
WebSockets and Use Cases
Today, browser implementations and libraries in all major programming languages and development frameworks are stable, reliable, and interoperable. This makes developing with WebSockets generally quite simple. Experienced developers reach for WebSockets in a wide variety of contexts and for a wide variety of use cases.
One specialized use case that isn’t a good fit for WebSockets, however, is sending and receiving real-time audio and video data.
Most real-time applications use WebSockets to set up each connection and manage application state, and use WebRTC to send and receiv audio and video.
For more about the differences between WebSockets and WebRTC read this article.
References:
- A good intro article showing how to use WebSockets in JavaScript
- RFC 6455 defines the WebSockets protocol
- A nice article from Mozilla detailing how to write a WebSocket server. Most developers will not ever need to do this, but it’s valuable (and fun) to understand how protocols like WebSockets work.
- A curated list of WebSockets implementations in various programming languages
Want to learn more about WebRTC? Visit our community, developer resources, or our blog.