• module

Wrap SocketIO client for SSR and testing.

The module exports a function that wraps to serve the following purposes:

stealSocket( url, [options] )

Since this is a wrapper around SocketIO io function it supports all the same arguments as does.

var io = require("");
var socket = io("localhost", {transports: ["websocket"]});


  1. url {String}

    A URL for connection.

  2. options {Object}Optional

    Optional parameters to be passed to



A proxy socket that can delay establishing connection.

Ignore SSR

If your application uses real-time communication with and your server supports SSR then its a good idea to ignore module during SSR completely.

The module maps to an @empty module, and stubs as minimally as possible.

Ignore can-zone

This wrapper is aware of can-zone module which helps to track asynchronous activity. It uses can-zone.ignore to skip the tracking of calls. For more information about what can-zone is checkout this article as well as the documentation.

Proxy and delay connection

This wrapper helps with testing and demoing applications that use for real-time communication.

Often some modules that use call it to establish a socket connection immediately. This makes mocking of impossible.

The wrapper delays the creation of socket connection till StealJS is done with loading all the modules (including the ones where we can mock

How it works

The delay-io wrapper returns io-like function that resolves with a ProxySocket. The ProxySocket is a replacement for io.Socket and acts as a proxy for it.

Initially the wrapper records all calls to io and its socket into a FIFO storage, and then, when StealJS is done with loading all modules, it replays the recorded calls against the real io and its socket.

After replaying the wrapper directly proxies all the subsequent calls.


Import which includes this wrapper as its part, or directly import to use just this wrapper.

Lets say we have an application myApp.js that uses and tries to establish the connection right during module evaluation. We import in our app instead of

var io = require("");

var messages = [];

var socket = io("localhost");
io.on("messages", function(m){

module.exports = {
    messages: messages

We now create a module myFixtureSocket.js that mocks server responses, e.g. using can-fixture-socket:

var io = require("");
var fixtureSocket = require("can-fixture-socket");
var mockSocket = new fixtureSocket( io );
mockSocket.on("connect", function(){
    mockSocket.emit("messages", "some messages");

And then we can test our application like this:

var myApp = require("my-app.js");

    assert.equal(myApp.messages.length, 1, "Contains one message received from socket server.");
Help us improve StealJS by taking our community survey