Home Reference Source
public class | source

Pubsub

The Pubsub client

Constructor Summary

Public Constructor
public

constructor(container: Container, internal: Boolean): Pubsub

Constructs a new Pubsub object.

Member Summary

Public Members
public get
public get

connected: Boolean

True if it is connected to the server.

Method Summary

Public Methods
public

close()

Closes connection.

public

Connects to server if the Skygear container has credentials and not connected.

public

hasHandlers(channel: String): boolean

Checks if the channel is subscribed with any handler.

public

off(channel: string, callback: function(object: *))

Unsubscribes a function callback on the specified channel.

public

on(channel: string, callback: function(object: *)): function(object: *)

Subscribes a function callback on receiving message at the specified channel.

public

onClose(listener: function()): EventHandle

Registers a connection close listener

public

onOpen(listener: function()): EventHandle

Registers a connection open listener

public

once(channel: string): Promise<Object>

Subscribes the channel for just one message.

public

publish(channel: String, data: Object)

Publishes message to a channel.

public

Connects to server if the Skygear container has credential, otherwise close the connection.

public

reset()

Closes connection and clear all handlers.

public

subscribe(channel: string, handler: function(object: *)): function(object: *)

Subscribes a function callback on receiving message at the specified channel.

public

unsubscribe(channel: string, handler: function(object: *))

Unsubscribes a function callback on the specified channel.

Public Constructors

public constructor(container: Container, internal: Boolean): Pubsub source

Constructs a new Pubsub object.

Params:

NameTypeAttributeDescription
container Container

the Skygear container

internal Boolean

true if it is an internal pubsub client

Return:

Pubsub

pubsub client

Public Members

public get WebSocket: WebSocket source

public get connected: Boolean source

True if it is connected to the server.

Public Methods

public close() source

Closes connection.

public connect() source

Connects to server if the Skygear container has credentials and not connected.

public hasHandlers(channel: String): boolean source

Checks if the channel is subscribed with any handler.

Params:

NameTypeAttributeDescription
channel String

name of the channel

Return:

boolean

true if the channel has handlers

public off(channel: string, callback: function(object: *)) source

Unsubscribes a function callback on the specified channel.

If pass in callback is null, all callbacks in the specified channel will be removed.

Params:

NameTypeAttributeDescription
channel string

name of the channel to unsubscribe

callback function(object: *)

function to be trigger with incoming data

public on(channel: string, callback: function(object: *)): function(object: *) source

Subscribes a function callback on receiving message at the specified channel.

Params:

NameTypeAttributeDescription
channel string

name of the channel to subscribe

callback function(object: *)

function to be trigger with incoming data

Return:

function(object: *)

The callback function

public onClose(listener: function()): EventHandle source

Registers a connection close listener

Params:

NameTypeAttributeDescription
listener function()

the listener

Return:

EventHandle

event handler

public onOpen(listener: function()): EventHandle source

Registers a connection open listener

Params:

NameTypeAttributeDescription
listener function()

the listener

Return:

EventHandle

event handler

public once(channel: string): Promise<Object> source

Subscribes the channel for just one message.

This function takes one message off from a pubsub channel, returning a promise of that message. When a message is received from the channel, the channel will be unsubscribed.

Params:

NameTypeAttributeDescription
channel string

name of the channel

Return:

Promise<Object>

promise of next message in this channel

public publish(channel: String, data: Object) source

Publishes message to a channel.

Params:

NameTypeAttributeDescription
channel String

name of the channel

data Object

data to be published

public reconfigure() source

Connects to server if the Skygear container has credential, otherwise close the connection.

public reset() source

Closes connection and clear all handlers.

public subscribe(channel: string, handler: function(object: *)): function(object: *) source

Subscribes a function callback on receiving message at the specified channel.

Params:

NameTypeAttributeDescription
channel string

name of the channel to subscribe

handler function(object: *)

function to be trigger with incoming data

Return:

function(object: *)

The callback function

public unsubscribe(channel: string, handler: function(object: *)) source

Unsubscribes a function callback on the specified channel.

If pass in callback is null, all callbacks in the specified channel will be removed.

Params:

NameTypeAttributeDescription
channel string

name of the channel to unsubscribe

handler function(object: *)
  • optional

function to be trigger with incoming data