new Client(service, endpoint, timeoutopt)
A Client is a remote interface
to a Service within a given endpoint.
See What's an endpoint?
for more information on 'endpoints'.
var endpoint = document.querySelector('iframe');
var client = bridge.client('my-service', endpoint);
Parameters
Name | Type | Attributes | Description |
---|---|---|---|
service |
String | The service name to connect to | |
endpoint |
Iframe | Worker | MessagePort | BroadcastChannel | Window | ||
timeout |
Number |
<optional> |
Override default response timeout The context/thread this service can be found in. |
Methods
connect()
Connect with the Service. Called
automatically internally, so
only required if you have
perposely called .disconnect().
disconnect()
Disconnect from the `Service`.
method(name, …argsopt) → {Promise}
Call a method on the connected Service.
client.method('greet', 'wilson').then(result => {
console.log(result); //=> 'hello wilson'
});
// my-service.js:
service.method('greet', name => {
return 'hello ' + name;
});
Parameters
Name | Type | Attributes | Description |
---|---|---|---|
name |
String | The method name | |
args |
* |
<optional> <repeatable> |
Arguments to send |
Returns
- Type
- Promise
plugin(fn) → {this}
Use a plugin with this Client.
See Writing plugins.
client.plugin(megaPlugin);
Parameters
Name | Type | Description |
---|---|---|
fn |
function | The plugin |
Returns
for chaining
- Type
- this
setPort(endpoint)
Set the port which all messages
will be sent over. This can differ
to the endpoint if we successfully
upgrade transport to MessageChannel.
Parameters
Name | Type | Description |
---|---|---|
endpoint |
Iframe | Worker | MessagePort | BroadcastChannel | Window |
destroy() → {Promise}
Destroy the Client. Waits from all
pending Messages to have responded.
client.destroy().then(() => ...);
Returns
- Type
- Promise
on(name, fn) → {this}
Listen to a Service .broadcast() or .push().
Services get notified whenever a Client
starts listening to a particular event.
client
.on('importantevent', data => ...)
.on('thingchanged', thing => ...);
Parameters
Name | Type | Description |
---|---|---|
name |
String | The event name |
fn |
function | Callback function |
Returns
for chaining
- Type
- this
off(name, fn) → {this}
Unlisten to a Service event.
client
.off('importantevent') // remove all
.off('thingchanged', onThingChanged); // remove one
Parameters
Name | Type | Description |
---|---|---|
name |
String | The event name |
fn |
function | Callback function |
This: Client
Returns
for chaining
- Type
- this