Hook.Channel.SSE Class
Item Index
Methods
close
()
Channel
Close event source connection.
Returns:
Channel:
this
disconnect
(
Hook.Channel
-
synchronous
Disconnect from channel, publishing a 'disconnected' message.
Parameters:
-
synchronous
Booleandefault = false
Returns:
Hook.Channel:
this
isConnected
()
Boolean
Is EventSource listenning to messages?
Returns:
Boolean:
publish
(
Promise
-
event
-
message
Publish event message
Parameters:
-
event
String -
message
Object
Returns:
Promise:
subscribe
(
Promise
-
event
-
callback
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