MessageClient Class
The MessageClient provides the entry point to the messaging functionality of the Sinch SDK. A MessageClient can be acquired via the SinchClient.
Note: Do not instantiate MessageClient, rather use the getMessageClient() method in SinchClient. See the example below.
Constructor
MessageClient
-
sinch
Parameters:
-
sinch
SinchClientThe parent object
Example:
//Get messageClient from sinchClient
var sinchClient = new SinchClient(...);
var messageClient = sinchClient.getMessageClient();
//Add event listener
messageClient.addEventListener(...);
//Create a new message
var message = messageClient.newMessage(...);
messageClient.send(message);
Item Index
Methods
ackMsg
-
recipientId
-
messageId
Internal method to take actions on reception of ack, for example, calling onDelivery handlers.
Parameters:
-
recipientId
StringThe id of the party sending the acknowledgement
-
messageId
Stringthe id of the message which was acknowledged
Returns:
undefined
addEventListener
-
eventListener
External method to add event listeners to the messageClient, multiple listeners can be added with this method. Listeners are processed in the order they're added. Listeners can be removed, using the MessageClient method.
Parameters:
-
eventListener
ObjectAn object containing a set of listeners for various actions
-
[onIncomingMessage]
Function optionalCallback for incoming message
-
[onMessageDelivered]
Function optionalCallback for delivered message
-
Returns:
undefined
Example:
var myListener = {
onIncomingMessage: function(message) { console.info(message); },
onMessageDelivered: function(message) { console.info(message); },
};
var messageClient = sinchClient.getMessageClient();
messageClient.addEventListener(myListener);
handleMessage
-
msgObj
Internal method to handle incoming message and take appropriate actions wrt. handlers / errors. Capable of recieving both loop-back messages from self and external messages.
Parameters:
-
msgObj
MXPMessageObjArriving object to be processed.
Returns:
Boolean defining if ack should be sent
newMessage
-
recipientIds
-
textBody
-
[publicHeaders]
Removes a previous added eventListener object.
Note: Do not send the same Message object more than once.
Parameters:
-
recipientIds
String | ArrayArray or string containing one or more recipientId's to send a message to.
-
textBody
StringThe message body to send
-
[publicHeaders]
Object optionalOpaque object sent with the message which can be inspected on the receiver side via Message.headers
Returns:
Newly created message object.
Example:
var messageClient = sinchClient.getMessageClient();
var message = messageClient.newMessage('recipient_username', 'Some awesome message');
messageClient.send(message);
removeEventListener
-
eventListener
External method to remove event lister objects from the messageClient. Pass the same object as was used when adding the listeners.
Parameters:
-
eventListener
ObjectAn object containing a set of listeners for various actions, that has previously been added to this messageClient
Returns:
undefined
Example:
messageClient.addEventListener(myListeners);
messageClient.removeEventListener(myListeners);
send
-
message
-
[success=console.info]
-
[fail=console.error]
Send an instant message. Can either be chained or supplied with success/fail callbacks. A promise will always be returned, supplied callback functions will be called before chained functions.
Note: If a user does not exist or a user is lacking the capability to receive your message, the error returned will inform you of what failed. Code 2000 for missing person, code 2001 for missing capability. The user(s) in question can be found in the error.response array.
Parameters:
-
message
MessageThe message object to send
-
[success=console.info]
Function optionalCallback for successfuly sent message (n.b. not necessarily delivered yet)
-
[fail=console.error]
Function optionalCallback for send error
Returns:
promise which resolves with the message
Example:
var messageClient = sinchClient.getMessageClient();
var message = messageClient.newMessage('foo', 'Hello World');
messageClient.send(message)
.then(function(message) {
//Successfully sent message (not delivered)
})
.fail(function(error) {
//Process error, can be that the user / one of the users don't exist.
})