Service

new Service(name)

A `Service` is a collection of methods exposed to a `Client`. Methods can be sync or async (using Promises).
bridge.service('my-service')
  .method('ping', param => 'pong: ' + param)
  .listen();
Parameters
Name Type Description
name String The service name

Extends

Methods

method(name, fn) → {this}

Define a method to expose to Clients. The return value of the result of a returned Promise will be sent back to the Client.
bridge.service('my-service')

  // sync return value
  .method('myMethod', function(param) {
    return 'hello: ' + param;
  })

  // or async Promise
  .method('myOtherMethod', function() {
    return new Promise(resolve => {
      setTimeout(() => resolve('result'), 1000);
    });
  })

  .listen();
Parameters
Name Type Description
name String
fn function
Returns
for chaining
Type
this

broadcast(type, dataopt, onlyopt) → {this}

Broadcast's an event from a `Service` to connected `Client`s. The third argument can be used to target selected clients by their `client.id`.
service.broadcast('my-event', { some: data }); // all clients
service.broadcast('my-event', { some: data }, [ clientId ]); // one client
Parameters
Name Type Attributes Description
type String The message type/name
data * <optional>
Data to send with the event
only Array <optional>
A select list of clients to message
Returns
Type
this

push(type, data, clientId, options) → {Promise}

Push message to a single connected Client.
client.on('my-event', data => ...)

...

service.push('my-event', { some: data}, clientId)
  .then(() => console.log('sent'));
Parameters
Name Type Description
type String
data Object
clientId String The Id of the Client to push to
options Object Optional parameters
Properties
Name Type Description
noResponse Boolean Tell the Client not to respond (Promise resolves instantly)
Returns
Type
Promise

plugin(fn) → {this}

Use a plugin with this Service.
Parameters
Name Type Description
fn function Plugin function
Returns
for chaining
Type
this

destroy()

Destroy the Service.

listen(thingopt)

Begin listening for inbound messages.
// When no arguments are given
// messages will be listened for
// on the default global scope
.listen();

// When an endpoint is out of reach
// BroadcastChannel can be used.
.listen(new BroadcastChannel('foo'));
Parameters
Name Type Attributes Description
thing HTMLIframeElement | Worker | MessagePort | BroadcastChannel | Window | Object <optional>
Inherited From:

unlisten()

Stop listening for inbound messages on all endpoints listened to prior.
Inherited From:

on(type, callback) → {this}

Add an event listener. It is possible to subscript to * events.
Parameters
Name Type Description
type String
callback function
Inherited From:
Returns
for chaining
Type
this

off(typeopt, callbackopt) → {this}

Remove an event listener.
emitter.off('name', fn); // remove one callback
emitter.off('name'); // remove all callbacks for 'name'
emitter.off(); // remove all callbacks
Parameters
Name Type Attributes Description
type String <optional>
callback function <optional>
Inherited From:
Returns
for chaining
Type
this

emit(type, dataopt) → {this}

Emit an event.
emitter.emit('name', { some: 'data' });
Parameters
Name Type Attributes Description
type String
data * <optional>
Inherited From:
Returns
for chaining
Type
this

Events

before-connect

Fires before the default 'connect' logic. This event acts as a hook for plugin authors to override default 'connect' behaviour.
service.on('before-connect', message => {
  message.preventDefault();
  // alternative connection logic ...
});
Parameters
Name Type Description
message Message The connect message

connected

Signals that a Client has connected.
service.on('connected', clientId => {
  console.log('client (%s) has connected', clientId);
});
Parameters
Name Type Description
clientId String The id of the connected Client

before-disconnect

Fires before the default 'disconnect' logic. This event acts as a hook for plugin authors to override default 'disconnect' behaviour.
service.on('before-disconnect', message => {
  message.preventDefault();
  // alternative disconnection logic ...
});
Parameters
Name Type Description
message Message The disconnect message

disconnected

Signals that a Client has disconnected.
service.on('disconnected', clientId => {
  console.log('client (%s) has disconnected', clientId);
});
Parameters
Name Type Description
clientId String The id of the disconnected Client

on

Signals that a Client has begun listening to a broadcast event.
service.on('on', data => {
  console.log('client (%s) is listening to %s', data.clientId, data.name);
});
Type:
  • Object
Properties:
Name Type Description
name String The broadcast name
clientId String The id of the Client that started listening

off

Signals that a Client has stopped listening to a broadcast event.
service.on('off', data => {
  console.log('client (%s) stopped listening to %s', data.clientId, data.name);
});
Parameters
Name Type Description
data Object
Properties
Name Type Description
name String The broadcast name
clientId String The id of the Client that stopped listening
Fork me on GitHub