new Chat(channelopt, isPrivateopt, autoConnectopt, metaopt, groupopt)
- Source:
This is the root Chat class that represents a chat room
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
channel |
String |
<optional> |
new Date().getTime()
|
A unique identifier for this chat Chat. Must be alphanumeric. The channel is the unique name of a Chat, and is usually something like "The Watercooler", "Support", or "Off Topic". See PubNub Channels. |
isPrivate |
Boolean |
<optional> |
false
|
Attempt to authenticate ourselves before connecting to this Chat. |
autoConnect |
Boolean |
<optional> |
true
|
Connect to this chat as soon as its initiated. If set to |
meta |
Object |
<optional> |
{}
|
Chat metadata that will be persisted on the server and populated on creation. |
group |
String |
<optional> |
'default'
|
Groups chat into a "type". This is the key which chats will be grouped into within Me.session object. |
Fires:
- $.ready
- $.state
- $.online.*
- $.offline.*
Extends
Members
asleep :Boolean
If user manually disconnects via ChatEngine#disconnect, the chat is put to "sleep". If a connection is reestablished via ChatEngine#reconnect, sleeping chats reconnect automatically.
Type:
- Boolean
(readonly) channel :String
- Source:
- See:
A string identifier for the Chat room. Any chat with an identical channel will be able to communicate with one another.
Type:
- String
connected :Boolean
Boolean value that indicates of the Chat is connected to the network
Type:
- Boolean
hasConnected :Boolean
Keep a record if we've every successfully connected to this chat before.
Type:
- Boolean
(readonly) isPrivate :Boolean
Excludes all users from reading or writing to the chat unless they have been explicitly invited via Chat#invite;
Type:
- Boolean
(readonly) meta :Object
Chat metadata persisted on the server. Useful for storing things like the name and description. Call Chat#update to update the remote information.
Type:
- Object
(readonly) users :Object
A list of users in this Chat. Automatically kept in sync as users join and leave the chat. Use $.join and related events to get notified when this changes
Type:
- Object
Methods
connect()
Establish authentication with the server, then subscribe with PubNub.
Example
// create a new chatroom, but don't connect to it automatically
let chat = new Chat('some-chat', false, false);
// connect to the chat when we feel like it
chat.connect();
Fires:
- $.ready
emit(event, data)
Send events to other clients in this User. Events are trigger over the network and all events are made on behalf of Me
Example
chat.emit('custom-event', {value: true});
chat.on('custom-event', (payload) => {
console.log(payload.sender.uuid, 'emitted the value', payload.data.value);
});
Parameters:
Name | Type | Description |
---|---|---|
event |
String | The event name |
data |
Object | The event payload object |
invite(user)
Invite a user to this Chat. Authorizes the invited user in the Chat and sends them an invite via User#direct.
Example
// one user running ChatEngine
let secretChat = new ChatEngine.Chat('secret-channel');
secretChat.invite(someoneElse);
// someoneElse in another instance of ChatEngine
me.direct.on('$.invite', (payload) => {
let secretChat = new ChatEngine.Chat(payload.data.channel);
});
Parameters:
Name | Type | Description |
---|---|---|
user |
User | The User to invite to this chatroom. |
Fires:
leave()
Example
chat.leave();
Fires:
objectify() → {Object}
Turns a Chat into a JSON representation.
Returns:
- Type
- Object
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:
- Overrides:
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 |
plugin(module)
- Source:
- Inherited From:
- Tutorials:
-
- Tutorial: using
Binds a plugin to this object
Parameters:
Name | Type | Description |
---|---|---|
module |
Object | The plugin module |
search(configopt) → {Search}
Search through previously emitted events. Parameters act as AND operators. Returns an instance of the emitter based History. Will
which will emit all old events unless config.event
is supplied.
Example
chat.search({
event: 'my-custom-event',
sender: ChatEngine.me,
limit: 20
}).on('my-custom-event', (event) => {
console.log('this is an old event!', event);
}).on('$.search.finish', () => {
console.log('we have all our results!')
});
Parameters:
Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Object |
<optional> |
Our configuration for the PubNub history request. See the PubNub History docs for more information on these parameters. Properties
|
Returns:
- Type
- Search
update(data)
Update the Chat metadata on the server.
Parameters:
Name | Type | Description |
---|---|---|
data |
object | JSON object representing chat metadta. |
Events
$.connected
Broadcast that the Chat is connected to the network.
Example
chat.on('$.connected', () => {
console.log('chat is ready to go!');
});
$.error.chatLeave
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
There was a problem leaving the chat.
$.error.chatMeta
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
Something went wrong updating the chat metadata.
$.error.getState
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
There was a problem restoring the state for this user
$.error.invite
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
Something went wrong with the server request to invite the user.
$.error.notConnected
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
There was a problem searching the chat.
$.error.presence
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
status |
Number | The PubNub service that surfaced this error |
error |
String | |
message |
String | The PubNub supplied error |
payload |
Object |
There was a problem fetching the presence of this chat
$.error.publish
There was a problem publishing over the PubNub network.
$.error.search
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
There was a problem fetching the history of this chat
$.error.sync
Properties:
Name | Type | Description |
---|---|---|
ceError |
String | The specific error thrown by ChatEngine |
There was a problem restoring the session
$.offline.disconnect
Properties:
Name | Type | Description |
---|---|---|
user |
Object | The User that disconnected |
Example
chat.on('$.offline.disconnect', (data) => {
console.log('User disconnected from the network:', data.user);
});
$.offline.leave
Properties:
Name | Type | Description |
---|---|---|
user |
User | The User that has left the room |
Example
chat.on('$.offline.leave', (data) => {
console.log('User left the room manually:', data.user);
});
$.online.here
Properties:
Name | Type | Description |
---|---|---|
user |
User | The User that came online |
Broadcast that a User has come online. This is when
the framework firsts learn of a user. This can be triggered
by, $.join
, or other network events that
notify the framework of a new user.
Example
chat.on('$.online.here', (data) => {
console.log('User has come online:', data.user);
});
$.online.join
Properties:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
data |
Object | The payload returned by the event Properties
|
Fired when a User has joined the room.
Example
chat.on('$.join', (data) => {
console.log('User has joined the room!', data.user);
});