meteor/sockjs-client

Name: sockjs-client

Owner: Meteor

Description: WebSocket emulation - Javascript client

Forked from: sockjs/sockjs-client

Created: 2017-11-10 00:35:44.0

Updated: 2017-11-10 00:35:46.0

Pushed: 2017-11-10 01:32:12.0

Homepage: http://sockjs.org

Size: 2285

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

SockJS-client

npm versionBuild StatusDependenciesChat Sauce Test Status

SockJS is a browser JavaScript library that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, Javascript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server.

Under the hood SockJS tries to use native WebSockets first. If that fails it can use a variety of browser-specific transport protocols and presents them through WebSocket-like abstractions.

SockJS is intended to work for all modern browsers and in environments which don't support the WebSocket protocol – for example, behind restrictive corporate proxies.

SockJS-client does require a server counterpart:

Philosophy:

Subscribe to SockJS mailing list for discussions and support.

SockJS family:

Work in progress:

Getting Started

SockJS mimics the WebSockets API, but instead of WebSocket there is a SockJS Javascript object.

First, you need to load the SockJS JavaScript library. For example, you can put that in your HTML head:

ipt src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>

After the script is loaded you can establish a connection with the SockJS server. Here's a simple example:

 sock = new SockJS('https://mydomain.com/my_prefix');
k.onopen = function() {
 console.log('open');
 sock.send('test');


k.onmessage = function(e) {
 console.log('message', e.data);
 sock.close();


k.onclose = function() {
 console.log('close');

SockJS-client API
SockJS class

Similar to the 'WebSocket' API, the 'SockJS' constructor takes one, or more arguments:

sockjs = new SockJS(url, _reserved, options);

url may contain a query string, if one is desired.

Where options is a hash which can contain:

Although the 'SockJS' object tries to emulate the 'WebSocket' behaviour, it's impossible to support all of its features. An important SockJS limitation is the fact that you're not allowed to open more than one SockJS connection to a single domain at a time. This limitation is caused by an in-browser limit of outgoing connections - usually browsers don't allow opening more than two outgoing connections to a single domain. A single SockJS session requires those two connections - one for downloading data, the other for sending messages. Opening a second SockJS session at the same time would most likely block, and can result in both sessions timing out.

Opening more than one SockJS connection at a time is generally a bad practice. If you absolutely must do it, you can use multiple subdomains, using a different subdomain for every SockJS connection.

Supported transports, by browser (html served from http:// or https://)

Browser | Websockets | Streaming | Polling —————-|——————|————-|——————- IE 6, 7 | no | no | jsonp-polling IE 8, 9 (cookies=no) | no | xdr-streaming † | xdr-polling † IE 8, 9 (cookies=yes)| no | iframe-htmlfile | iframe-xhr-polling IE 10 | rfc6455 | xhr-streaming | xhr-polling Chrome 6-13 | hixie-76 | xhr-streaming | xhr-polling Chrome 14+ | hybi-10 / rfc6455| xhr-streaming | xhr-polling Firefox <10 | no ‡ | xhr-streaming | xhr-polling Firefox 10+ | hybi-10 / rfc6455| xhr-streaming | xhr-polling Safari 5.x | hixie-76 | xhr-streaming | xhr-polling Safari 6+ | rfc6455 | xhr-streaming | xhr-polling Opera 10.70+ | no ‡ | iframe-eventsource | iframe-xhr-polling Opera 12.10+ | rfc6455 | xhr-streaming | xhr-polling Konqueror | no | no | jsonp-polling

Supported transports, by browser (html served from file://)

Sometimes you may want to serve your html from “file://” address - for development or if you're using PhoneGap or similar technologies. But due to the Cross Origin Policy files served from “file://” have no Origin, and that means some of SockJS transports won't work. For this reason the SockJS transport table is different than usually, major differences are:

Browser | Websockets | Streaming | Polling —————-|—————|——————–|——————- IE 8, 9 | same as above | iframe-htmlfile | iframe-xhr-polling Other | same as above | iframe-eventsource | iframe-xhr-polling

Supported transports, by name

Transport | References ———————|————— websocket (rfc6455) | rfc 6455 websocket (hixie-76) | draft-hixie-thewebsocketprotocol-76 websocket (hybi-10) | draft-ietf-hybi-thewebsocketprotocol-10 xhr-streaming | Transport using Cross domain XHR streaming capability (readyState=3). xdr-streaming | Transport using XDomainRequest streaming capability (readyState=3). eventsource | EventSource/Server-sent events. iframe-eventsource | EventSource/Server-sent events used from an iframe via postMessage. htmlfile | HtmlFile. iframe-htmlfile | HtmlFile used from an iframe via postMessage. xhr-polling | Long-polling using cross domain XHR. xdr-polling | Long-polling using XDomainRequest. iframe-xhr-polling | Long-polling using normal AJAX from an iframe via postMessage. jsonp-polling | Slow and old fashioned JSONP polling. This transport will show “busy indicator” (aka: “spinning wheel”) when sending data.

Connecting to SockJS without the client

Although the main point of SockJS is to enable browser-to-server connectivity, it is possible to connect to SockJS from an external application. Any SockJS server complying with 0.3 protocol does support a raw WebSocket url. The raw WebSocket url for the test server looks like:

You can connect any WebSocket RFC 6455 compliant WebSocket client to this url. This can be a command line client, external application, third party code or even a browser (though I don't know why you would want to do so).

Deployment

You should use a version of sockjs-client that supports the protocol used by your server. For example:

ipt src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>

For server-side deployment tricks, especially about load balancing and session stickiness, take a look at the SockJS-node readme.

Development and testing

SockJS-client needs node.js for running a test server and JavaScript minification. If you want to work on SockJS-client source code, checkout the git repo and follow these steps:

cd sockjs-client
npm install

To generate JavaScript, run:

gulp browserify

To generate minified JavaScript, run:

gulp browserify:min

Both commands output into the build directory.

Testing

Once you've compiled the SockJS-client you may want to check if your changes pass all the tests.

npm run test:browser_local

This will start zuul and a test support server. Open the browser to http://localhost:9090/_zuul and watch the tests run.

Browser Quirks

There are various browser quirks which we don't intend to address:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.