gitmyhub

sockjs-client

JavaScript ★ 8.5k updated 7d ago

WebSocket emulation - Javascript client

SockJS-client is a JavaScript browser library that enables reliable real-time two-way communication with a server, automatically falling back from WebSockets to other methods when WebSockets are blocked or unavailable.

JavaScriptnpmsetup: easycomplexity 2/5

SockJS-client is a JavaScript library for web browsers that enables real-time, two-way communication between a browser and a server. The standard way to do this on the modern web is a technology called WebSockets, but WebSockets do not work everywhere: some corporate networks, older browsers, and certain server configurations block or do not support them. SockJS solves this by trying WebSockets first and automatically falling back to other communication methods if that does not work, all while giving the developer a single consistent API to program against.

From a developer's perspective, writing code with SockJS looks almost identical to writing code with WebSockets directly. You create a SockJS object, then respond to events like "connection opened," "message received," and "connection closed." The library handles the underlying transport negotiation invisibly. This means applications built on SockJS tend to be more reliable across different network environments without extra code from the developer.

SockJS-client is the browser half of the system. It requires a compatible server-side counterpart to work. The README lists server implementations in a wide range of languages and frameworks including Node.js, Python (multiple options), Java (Spring, Vert.x, Atmosphere), Erlang, Scala, Rust, and others, which means it can be paired with many different backend technology stacks.

The library was designed with a few specific principles: no dependency on Flash (which avoided a common compatibility problem from earlier web eras), support for cross-domain connections, and fast connection setup. It is available through the npm package registry and can also be loaded directly from a CDN by including a script tag in an HTML page.

Where it fits