ChatEngine

ChatEngine

new ChatEngine() → {ChatEngine}

Source:
Returns:

Returns an instance of ChatEngine

Type
ChatEngine

Extends

Members

(static) Chat :Chat

Source:
See:

The Chat class. Creates a new Chat when initialized, or returns an existing instance if chat has already been created.

Type:

(static) chats :Object

Source:

A map of all known Chats in this instance of ChatEngine.

Type:
  • Object

(static) global :Chat

Source:

A global Chat that all Users join when they connect to ChatEngine. Useful for announcements, alerts, and global events.

Type:

(static) me :Me

Source:

This instance of ChatEngine represented as a special User know as Me.

Type:

(static) pubnub :Object

Source:

An instance of PubNub, the networking infrastructure that powers the realtime communication between Users in Chats.

Type:
  • Object

(static) ready :Object

Source:

Indicates if ChatEngine has fired the ChatEngine#$"."ready event.

Type:
  • Object

(static) User :User

Source:
See:

The User class. Creates a new User when initialized, or returns an existing instance if chat has already been created.

Type:

(static) users :Object

Source:

A map of all known Users in this instance of ChatEngine.

Type:
  • Object

Methods

connect(uuid, stateopt, authKeyopt)

Source:

Connect to realtime service and create instance of Me

Parameters:
Name Type Attributes Default Description
uuid String

A unique string for Me. It can be a device id, username, user id, email, etc. Must be alphanumeric.

state Object <optional>
{}

An object containing information about this client (Me). This JSON object is sent to all other clients on the network, so no passwords!

authKey String <optional>

A authentication secret. Will be sent to authentication backend for validation. This is usually an access token. See auth for more.

Fires:
  • $.connected

disconnect()

Source:

Disconnect from all Chats and mark them as asleep.

Example
// create a new chat
let chat = new ChatEngine.Chat(new Date().getTime());

// disconnect from ChatEngine
ChatEngine.disconnect();

// every individual chat will be disconnected
chat.on('$.disconnected', () => {
    done();
});

// Changing User:
ChatEngine.disconnect()
ChatEngine = new ChatEngine({}, {});
ChatEngine.connect()

off(event)

Source:
Inherited From:

Stop a callback from listening to an event.

Example
let callback = function(payload;) {
   console.log('something happend!');
};
object.on('event', callback);
// ...
object.off('event', callback);
Parameters:
Name Type Description
event String

The event name

on(event, cb)

Source:
Inherited From:

Listen for a specific event and fire a callback when it's emitted. Supports wildcard matching.

Example
// Get notified whenever someone joins the room
object.on('event', (payload) => {
    console.log('event was fired').
})

// Get notified of event.a and event.b
object.on('event.*', (payload) => {
    console.log('event.a or event.b was fired').;
})
Parameters:
Name Type Description
event String

The event name

cb function

The function to run when the event is emitted

onAny(callback)

Source:
Inherited From:

Listen for any event on this object and fire a callback when it's emitted

Example
object.onAny((event, payload) => {
    console.log('All events trigger this.');
});
Parameters:
Name Type Description
callback function

The function to run when any event is emitted. First parameter is the event name and second is the payload.

once(event, callback)

Source:
Inherited From:

Listen for an event and only fire the callback a single time

Example
object.once('message', => (event, payload) {
    console.log('This is only fired once!');
});
Parameters:
Name Type Description
event String

The event name

callback function

The function to run once

proto(className, plugin)

Source:

Bind a plugin to all future instances of a Class.

Parameters:
Name Type Description
className String

The string representation of a class to bind to

plugin Class

The plugin function.

reauthorize()

Source:

Disconnects, changes authentication token, performs handshake with server and reconnects with new auth key. Used for extending logged in sessions for active users.

Example
// early
ChatEngine.connect(...);

ChatEngine.once('$.connected', () => {
    // first connection established
});

// some time passes, session token expires
ChatEngine.reauthorize(authKey);

// we are connected again
ChatEngine.once('$.connected', () => {
    // we are connected again
});

reconnect()

Source:

Performs authentication with server and restores connection to all sleeping chats.

Example
// create a new chat
let chat = new ChatEngine.Chat(new Date().getTime());

// disconnect from ChatEngine
ChatEngine.disconnect();

// reconnect sometime later
ChatEngine.reconnect();

Events

$.created.chat

Source:

Fired when a Chat has been created within ChatEngine.

Example
ChatEngine.on('$.created.chat', (data, chat) => {
    console.log('Chat was created', chat);
});

$.created.me

Source:

Fired when a Me has been created within ChatEngine.

Example
ChatEngine.on('$.created.me', (data, me) => {
    console.log('Me was created', me);
});

$.created.user

Source:

Fired when a User has been created within ChatEngine.

Example
ChatEngine.on('$.created.user', (data, user) => {
    console.log('Chat was created', user);
});

$.network.down.badrequest

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

Server rejected the request.

$.network.down.decryption

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

If using decryption strategies and the decryption fails.

$.network.down.denied

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

PAM permission failure.

$.network.down.issue

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

A subscribe event experienced an exception when running.

$.network.down.malformed

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

JSON parsing crashed.

$.network.down.offline

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

SDK detected that network is down.

$.network.down.timeout

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

Request timed out.

$.network.up.connected

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

SDK subscribed with a new mix of channels.

$.network.up.online

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

operation String

The PubNub operation attempted to be performed.

affectedChannelGroups Array
affectedChannel Array
subscribedChannels Array
lastTimetoken String
currentTimetoken String

SDK detected that network is online.

$.network.up.reconnected

Source:
Properties:
Name Type Description
category String

The PubNub statusEvent category.

SDK was able to reconnect to pubnub.

$.ready

Source:
Properties:
Name Type Description
me Me

The ChatEngine Me object representing this client.

Fired when ChatEngine is connected to the internet and ready to go!

Example
ChatEngine.on('$.ready', (data) => {
    let me = data.me;
})

$.state

Source:
Properties:
Name Type Description
user User

The User that changed state

state Object

The new user state

Broadcast that a User has changed state.

Example
ChatEngine.on('$.state', (data) => {
    console.log('User has changed state:', data.user, 'new state:', data.state);
});