ShareJSWrapper

ShareJSWrapper

A wrapper around a ShareJS client, providing easier use for a single document per connection

Constructor

new ShareJSWrapper(config)

Parameters
Name Type Description
config Object

An object of configuration options for this client

Properties
Name Type Description
accessToken string

A Canvas API authentication token

canvasID string

An ID of a Canvas to connect to

realtimeURL string

The URL of the realtime server

orgID string

The ID of the org the canvas belongs to

Members

content :string

The current content of the ShareJS document

Methods

connect(callback)

Tell the wrapper client to connect to the configured ShareJS server.

Example
share.connect(function onConnected() {
  console.log(share.content);
});
Parameters
Name Type Description
callback ShareJSWrapper~connectCallback

A callback to call once connected

disconnect()

Tell the wrapper client to close the ShareJS connection and socket.

Example
share.disconnect();

insert(offset, text)

Send an insert operation from this client to the server.

Example
share.insert(10, 'Foo');
Parameters
Name Type Description
offset number

The offset at which to start the insert operation

text string

The text to be inserted

on(event, fn, context) → {ShareJSWrapper}

Add a listener to an event.

Example
share.on('connect', function onConnect() {
  // ...
});
Parameters
Name Type Description
event string

The name of the event

fn function

The function to call when the event occurs

context *

The context on which to call the function

Returns

This same instance

Type
ShareJSWrapper

once(event, fn, context) → {ShareJSWrapper}

Add a listener to an event which will fire once.

Example
share.once('connect', function onConnect() {
  // ...
});
Parameters
Name Type Description
event string

The name of the event

fn function

The function to call when the event occurs

context *

The context on which to call the function

Returns

This same instance

Type
ShareJSWrapper

remove(start, length)

Send a remove operation from this client to the server.

Example
share.remove(0, 3);
Parameters
Name Type Description
start number

The position at which to start the remove

length number

The number of characters to remove

removeAllListeners(eventopt) → {ShareJSWrapper}

Remove all listeners or only for the specified event.

Example
share.removeAllListeners();
Parameters
Name Type Attributes Description
event string <optional>

The event to remove all listeners for

Returns

This same instance

Type
ShareJSWrapper

removeListener(event, fnopt, contextopt, once) → {ShareJSWrapper}

Remove a listener from an event.

Example
share.removeListener('connect', connectHandler, this, true);
Parameters
Name Type Attributes Description
event string

The event to remove listener(s) from

fn function <optional>

The listener to remove

context * <optional>

Only match listeners matching this context

once boolean

Only remove "once" listeners

Returns

This same instance

Type
ShareJSWrapper

Type Definitions

connectCallback()

A callback called on successful connection after a connect call

Insert

A two-element array consisting of a length of text to retain and a string to insert

Example
[10, "Foo"]

Remove

A two-element array consisting of a length of text to retain and a length of text to remove

Example
[10, 12]

Events

disconnect

An event emitted when the client has fatally disconnected and will no longer attempt reconnects.

Check err.message for "abnormal", "no_pong", "forbidden", "not_found", or "unexpected".

Type:
  • Error

insert

An event emitted when the client receives an insert operation. Emits an event with an exploded Insert operation.

Parameters
Name Type Description
retain number

The length being retained in the insert

value string

The value being inserted

remove

An event emitted when the client receives a remove operation. Emits an exploded Remove operation.

Parameters
Name Type Description
retain number

The length being retained in the insert

value number

The length being removed