Home Reference Source
import {WebSocketBridge} from 'django_redux/lib/index'
public class | source

WebSocketBridge

Direct Subclass:

ReduxBridge

Bridge between Channels and plain javascript.

Example:

const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.listen(function(action, stream) {
  console.log(action, stream);
});

Method Summary

Public Methods
public

connect(url: String, protocols: String[] | String, options: Object)

Connect to the websocket server

public

demultiplex(stream: String, cb: Function)

Adds a 'stream handler' callback.

public

Starts listening for messages on the websocket, demultiplexing if necessary.

public

send(msg: Object)

Sends a message to the reply channel.

public

stream(stream: String): Object

Returns an object to send messages to a specific stream

Public Methods

public connect(url: String, protocols: String[] | String, options: Object) source

Connect to the websocket server

Params:

NameTypeAttributeDescription
url String
  • optional

The url of the websocket. Defaults to window.location.host

protocols String[] | String
  • optional

Optional string or array of protocols.

options Object

Object of options for reconnecting-websocket.

Example:

const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();

public demultiplex(stream: String, cb: Function) source

Adds a 'stream handler' callback. Messages coming from the specified stream will call the specified callback.

Params:

NameTypeAttributeDescription
stream String

The stream name

cb Function

Callback to be execute when a message arrives. The callback will receive action and stream parameters.

Example:

const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.listen();
webSocketBridge.demultiplex('mystream', function(action, stream) {
  console.log(action, stream);
});
webSocketBridge.demultiplex('myotherstream', function(action, stream) {
  console.info(action, stream);
});

public listen(cb: Function) source

Starts listening for messages on the websocket, demultiplexing if necessary.

Params:

NameTypeAttributeDescription
cb Function
  • optional

Callback to be execute when a message arrives. The callback will receive action and stream parameters

Example:

const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.listen(function(action, stream) {
  console.log(action, stream);
});

public send(msg: Object) source

Sends a message to the reply channel.

Params:

NameTypeAttributeDescription
msg Object

The message

Example:

// We cheat by using the Redux-style Actions as our
// communication protocol with the server. Consider separating
// communication format from client-side action API.
webSocketBridge.send({type: 'MYACTION', 'payload': 'somepayload'});

public stream(stream: String): Object source

Returns an object to send messages to a specific stream

Params:

NameTypeAttributeDescription
stream String

The stream name

Return:

Object

convenience object to send messages to stream.

Example:

// We cheat by using the Redux-style Actions as our
// communication protocol with the server. Consider separating
// communication format from client-side action API.
webSocketBridge.stream('mystream').send({type: 'MYACTION', 'payload': 'somepayload'})