API Docs for: 0.2.1
Show:

Hook.Channel.SSE Class

Module: Hook

Methods

close

() Channel

Close event source connection.

Returns:

Channel:

this

disconnect

(
  • synchronous
)
Hook.Channel

Disconnect from channel, publishing a 'disconnected' message.

Parameters:

  • synchronous Boolean

    default = false

Returns:

Hook.Channel:

this

isConnected

() Boolean

Is EventSource listenning to messages?

Returns:

Boolean:

publish

(
  • event
  • message
)
Promise

Publish event message

Parameters:

  • event String
  • message Object

Returns:

Promise:

subscribe

(
  • event
  • callback
)
Promise

Subscribe to channel. Publishes a 'connected' message on the first time.

Parameters:

  • event String

    (optional)

  • callback Function

Returns:

Promise:

Example:

Registering for all messages

channel.subscribe(function(event, data) {
  console.log("Message: ", event, data);
})

Registering for a single custom event

channel.subscribe('some-event', function(data) {
  console.log("Custom event triggered: ", data);
})

Registering for client connected/disconnected events

channel.subscribe('connected', function(data) {
  console.log("New client connected: ", data.client_id);
});
channel.subscribe('disconnected', function(data) {
  console.log("Client disconnected: ", data.client_id);
});

Registering error event

channel.subscribe('state:open', function(e) {
  console.log("Error: ", e);
});
channel.subscribe('state:error', function(e) {
  console.log("Error: ", e);
});

unsubscribe

(
  • event
)

Unsubscribe to a event listener

Parameters:

  • event String