SkygearChatContainer
SkygearChatContainer provide API access to the chat plugin.
Member Summary
Public Members | ||
public get |
pubsub: * |
Method Summary
Public Methods | ||
public |
addAdmins allow adding admins to a conversation. |
|
public |
addParticipants(conversation: Conversation, participants: []User): Promise<Conversation> addParticipants allow adding participants to a conversation. |
|
public |
createConversation(participants: []User, title: string, meta: object, options: object): Promise<Conversation> createConversation create an conversation with provided participants and title. |
|
public |
createDirectConversation(user: User, title: string, meta: object, options: object): Promise<Conversation> createDirectConversation is a helper function will create conversation with distinctByParticipants set to true |
|
public |
createMessage(conversation: Conversation, body: string, metadata: object, asset: File): Promise<Message> createMessage create a message in a conversation. |
|
public |
getConversation(conversationID: string): Promise<Conversation> getConversation query a Conversation Record from Skygear |
|
public |
getConversations(): Promise<[]Conversation> getConversation query a list of Conversation Records from Skygear which are readable to the current user |
|
public |
getMessages(conversation: Conversation, limit: number, beforeTime: Date): Promise<[]Message> getMessages return an array of message in a conversation. |
|
public |
getUnreadCount return following unread count; |
|
public |
getUnreadMessageCount(conversation: Conversation): Promise<number> getUnreadMessageCount query a unread count of a conversation |
|
public |
getUserConversation(conversation: Conversation, includeLastMessage: boolean): Promise<UserConversation> getUserConversation query a UserConversation record of current logged in user and the pass in Conversation. |
|
public |
getUserConversations(includeLastMessage: boolean): Promise<[]UserConversation> getUserConversations query all UserConversation record of current logged in user. |
|
public |
leaveConversation(conversation: Conversation): Promise<boolean> Leave a conversation. |
|
public |
markAsDelivered(messages: []Message): Promise<boolean> markAsDelivered mark all messages as delivered |
|
public |
markAsLastMessageRead(conversation: Conversation, message: Message): Promise<number> markAsLastMessageRead mark the message as last read message. |
|
public |
markAsRead(messages: []Message): Promise<boolean> markAsRead mark all messages as read |
|
public |
removeAdmins(conversation: Conversation, admins: []User): Promise<Conversation> removeParticipants allow removal of participants from a conversation. |
|
public |
removeParticipants(conversation: Conversation, participants: []User): Promise<COnversation> removeParticipants allow removal of participants from a conversation. |
|
public |
sendTypingIndicator(conversation: Conversation, state: string): Promise<number> sendTypingIndicaton send typing indicator to the specified conversation. |
|
public |
subscribe all message changes event from the system. |
|
public |
subscribeAllTypingIndicator(callback: function) Subscribe to typing indicator events in all conversation. |
|
public |
subscribeTypingIndicator(conversation: Conversation, callback: function) Subscribe to typing indicator events in a conversation. |
|
public |
unsubscribe(handler: function) Unsubscribe one or all typing message handler(s) |
|
public |
unsubscribeTypingIndicator(conversation: Conversation, handler: function) unsubscribe one or all typing indicator handler(s) from a conversation. |
|
public |
updateConversation(conversation: Conversation, title: string, meta: object): Promise<Conversation> updateConversation is a helper method for updating a conversation with the provied title and meta. |
Public Members
public get pubsub: * source
Public Methods
public addAdmins(conversation: Conversation, admins: []User): Promise<Conversation> source
addAdmins allow adding admins to a conversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | Conversation to update |
|
admins | []User | array of Skygear User |
public addParticipants(conversation: Conversation, participants: []User): Promise<Conversation> source
addParticipants allow adding participants to a conversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | Conversation to update |
|
participants | []User | array of Skygear User |
public createConversation(participants: []User, title: string, meta: object, options: object): Promise<Conversation> source
createConversation create an conversation with provided participants and title.
Duplicate call of createConversation with same list of participants will
return the different conversation, unless distinctByParticipants
in
options is set to true. By default distinctByParticipants
is false.
Adding or removing participants from a distinct conversation (see below) makes it non-distinct.
For application specific attributes, you are suggested to put them as meta.
All participant will be admin unless specific in options.admins
Example:
const skygearChat = require('skygear-chat');¬
skygearChat.createConversation([userBen], 'Greeting')
.then(function (conversation) {
console.log('Conversation created!', conversation);
}, function (err) {
console.log('Conversation created fails');
});
public createDirectConversation(user: User, title: string, meta: object, options: object): Promise<Conversation> source
createDirectConversation is a helper function will create conversation with distinctByParticipants set to true
Example:
const skygearChat = require('skygear-chat');¬
skygearChat.createDirectConversation(userBen, 'Greeting')
.then(function (conversation) {
console.log('Conversation created!', conversation);
}, function (err) {
console.log('Conversation created fails');
});
public createMessage(conversation: Conversation, body: string, metadata: object, asset: File): Promise<Message> source
createMessage create a message in a conversation.
A message can be just a text message, or a message with image, audio or video attachment. Application developer can also save metadata to a message, so the message can be display as important notice. The metadata provide flexibility to application to control how to display the message, like font and color.
Example:
const skygearChat = require('skygear-chat');
skygearChat.createMessage(
conversation,
'Red in color, with attachment',
{'color': 'red', },
$('message-asset').files[0],
).then(function (result) {
console.log('Save success', result);
});
public getConversation(conversationID: string): Promise<Conversation> source
getConversation query a Conversation Record from Skygear
Params:
Name | Type | Attribute | Description |
conversationID | string | ConversationID |
public getConversations(): Promise<[]Conversation> source
getConversation query a list of Conversation Records from Skygear which are readable to the current user
public getMessages(conversation: Conversation, limit: number, beforeTime: Date): Promise<[]Message> source
getMessages return an array of message in a conversation. The way of
query is to provide limit
and beforeTime
. The expected way is to
query from the latest message first. And use the message createdAt
to
query the next pages via setting beforeTime
when user scroll.
Once you query specific messages, the SDK will automatically mark the message as delivery on the server.
Example:
const skygearChat = require('skygear-chat');¬
const ulNode = document.createElement('UL');
const currentTime = new Date();
skygearChat.getMessages(conversation, 10, currentTime)
.then(function (messages) {
let lastMsgTime;
message.forEach(function (m) {
const liNode = document.createElement('LI');
liNode.appendChild(document.createTextNode(m.content));
ulNode.appendChild(liNode);
lastMsgTime = m.createAt;
});
// Querying next page
skygearChat.getMessages(conversation, 10, lastMsgTime).then(...);
}, function (err) {
console.log('Error: ', err);
});
public getUnreadCount(): Promise<object> source
getUnreadCount return following unread count;
- The total unread message count of current user.
- The total number of conversation have one or more unread message.
Format is as follow:
{
'conversation': 3,
'message': 23
}
Example:
const skygearChat = require('skygear-chat');¬
skygearChat.getUnreadCount().then(function (count) {
console.log('Total message unread count: ', count.message);
console.log(
'Total converation have unread message: ',
count.conversation);
}, function (err) {
console.log('Error: ', err);
});
public getUnreadMessageCount(conversation: Conversation): Promise<number> source
getUnreadMessageCount query a unread count of a conversation
Params:
Name | Type | Attribute | Description |
conversation | Conversation | conversation to be query |
public getUserConversation(conversation: Conversation, includeLastMessage: boolean): Promise<UserConversation> source
getUserConversation query a UserConversation record of current logged in user and the pass in Conversation.
The UserConversation will transientInclude Conversion and User object for ease of use.
For transientInclude of last_read_message
, we provided an boolean flag
to include or not. This function will transientInclude the it unless
specified otherwise.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | Conversation |
|
includeLastMessage | boolean | Transient include the
|
public getUserConversations(includeLastMessage: boolean): Promise<[]UserConversation> source
getUserConversations query all UserConversation record of current logged in user.
UserConversation is a Skygear Record contain user specific data to a
conversation, like unread
count, last_message
. This method will
return all UserConversation records assoicated to the current user. The
UserConversation will transientInclude Conversion and User object for
ease of use.
For transientInclude of last_message
, we provided an boolean flag
to include or not. This function will transientInclude the it unless
specified otherwise.
Params:
Name | Type | Attribute | Description |
includeLastMessage | boolean | Transient include the
|
Example:
const skygearChat = require('skygear-chat');¬
const ulNode = document.createElement('UL');
skygearChat.getUserConversation()
.then(function (userConversations) {
userConversations.forEach(function (uc) {
const liNode = document.createElement('LI');
liNode.appendChild(document.createTextNode(uc.conversation.title));
liNode.appendChild(document.createTextNode(uc.unread));
ulNode.appendChild(liNode);
});
}, function (err) {
console.log('Cannot load conversation list');
});
public leaveConversation(conversation: Conversation): Promise<boolean> source
Leave a conversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | Conversation to leave |
public markAsDelivered(messages: []Message): Promise<boolean> source
markAsDelivered mark all messages as delivered
Params:
Name | Type | Attribute | Description |
messages | []Message | an array of message to mark as delivery |
public markAsLastMessageRead(conversation: Conversation, message: Message): Promise<number> source
markAsLastMessageRead mark the message as last read message. Once you mark a message as last read, the system will update the unread count at UserConversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | conversation the message belong to |
|
message | Message | message to be mark as last read |
public markAsRead(messages: []Message): Promise<boolean> source
markAsRead mark all messages as read
Params:
Name | Type | Attribute | Description |
messages | []Message | an array of message to mark as read |
public removeAdmins(conversation: Conversation, admins: []User): Promise<Conversation> source
removeParticipants allow removal of participants from a conversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | Conversation to update |
|
admins | []User | array of Skygear User |
public removeParticipants(conversation: Conversation, participants: []User): Promise<COnversation> source
removeParticipants allow removal of participants from a conversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | Conversation to update |
|
participants | []User | array of Skygear User |
public sendTypingIndicator(conversation: Conversation, state: string): Promise<number> source
sendTypingIndicaton send typing indicator to the specified conversation.
The state can be begin
, pause
and finished
.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | conversation to be query |
|
state | string | the state to send |
public subscribe(handler: function) source
subscribe all message changes event from the system.
The server will push all messsage change events via UserChannel that concerning the current user. i.e. all message belongs to a conversation that the current user have access to.
The handler will receive following object as parameters
{
"record_type": "message",
"event_type": "create",
"record": recordObj,
"original_record": nulll
}
event_type
can beupdate
,create
anddelete
.recordObj
isskygear.Record
instance.
Common use-case on the event_type:
create
- other user send a message to the conversation and insert it in
the conversation view.
updated
- when a message is received by other, the message delivery
status is changed. For example, from delivered
to some_read
. You can
check the conversation_status
fields to see the new delivery status.
Params:
Name | Type | Attribute | Description |
handler | function | function to be invoke when a notification arrive |
public subscribeAllTypingIndicator(callback: function) source
Subscribe to typing indicator events in all conversation.
If you application want to dispatch the typing other than
per-conversation manner. You can use this method in stead of
subscribeTypingIndicator
.
The format of payload is similiar with conversation id as key to separate users' typing event. To get typing indicator event, call this method with a handler that accepts following parameters.
{
"conversation/id1": {
"user/id": {
"event": "begin",
"at": "20161116T78:44:00Z"
},
"user/id2": {
"event": "begin",
"at": "20161116T78:44:00Z"
}
}
}
Params:
Name | Type | Attribute | Description |
callback | function | function be be invoke when there is someone typing in conversation you have access to. |
public subscribeTypingIndicator(conversation: Conversation, callback: function) source
Subscribe to typing indicator events in a conversation.
You are required to specify a conversation where typing indicator events apply. You may subscribe to multiple conversation at the same time. To get typing indicator event, call this method with a handler that accepts following parameters.
{
"user/id": {
"event": "begin",
"at": "20161116T78:44:00Z"
},
"user/id2": {
"event": "begin",
"at": "20161116T78:44:00Z"
}
}
Params:
Name | Type | Attribute | Description |
conversation | Conversation | conversation to be query |
|
callback | function | function be be invoke when there is someone typing in the specificed conversation |
public unsubscribe(handler: function) source
Unsubscribe one or all typing message handler(s)
Params:
Name | Type | Attribute | Description |
handler | function | Which handler to remove, if absent, all handlers are removed. |
public unsubscribeTypingIndicator(conversation: Conversation, handler: function) source
unsubscribe one or all typing indicator handler(s) from a conversation.
Params:
Name | Type | Attribute | Description |
conversation | Conversation | conversation to be unsubscribe |
|
handler | function | Which handler to remove, if absent, all handlers are removed. |