Skip to content

Connecting to SockJS without the browser

Marek Majkowski edited this page Mar 21, 2012 · 5 revisions

Connecting to SockJS without the browser

Raw WebSockets

If you wish to connect to a SockJS server without using the [SockJS-client](https://github.com/sockjs/sockjs-client), you probably should use Raw WebSockets interface.

SockJS 0.2 and newer have a special url, which exposes SockJS a connection by using pure native WebSockets without using any custom SockJS framing. As simple as that. Any RFC 6455 compliant WebSocket library should be able to access SockJS server.

For example, the raw websocket url may look like this:

  • ws://localhost:8081/echo/websocket

For details see "Connecting to SockJS without the client" section in the [SockJS-Client README](https://github.com/sockjs/sockjs-client#readme).

Using HTTP streaming

If you wish to use HTTP streaming, you would need to write a more complex SockJS client.

General flow for receiving data from xhr-streaming in SockJS goes like that:

  • Client opens a receiver request to http://localhost:8081/echo/<srv_id>/<conn_id>/xhr_streaming
  • The request will be hanging and the server will write data as Http chunks on it.
  • Client can now receive frames from the server.
  • The connection at any point may be closed by a server. In such case the client must reestablish connection.

Some assumptions regarding the framing:

  • In case the client receives the "open" frame for the second time during one session it must abort the session.
  • After receiving the close frame the client must abort the session.

Flow for sending data:

  • Once the receiver receives an "open" frame the client may send data to http://localhost:8081/echo/<srv_id>/<conn_id>/xhr_send
  • To avoid delivering messages out of order the client is allowed to have at most one sending request at a time. If the application wants to send a message while the send request is in progress the client must buffer the message.
  • If the send request will finish with other status code than 200 or 204, the session must be aborted.

Additional warnings:

  • If you wish to avoid the need of parsing gzip, suplly: Accept: identity header on all http requests.
  • The client _must_ remember cookies set by the server and echo them back on following requests. This is very important, the client must behave like a browser in that regard.
  • Be aware that SockJS uses non-standard JSON encoding of various weird unicode characters. Your client should accept any character to be encoded as "uXXXX" and, if possible, should escape characters as defined by the sockjs-protocol.

For details please see: