API Docs for: 0.9.9
Show:

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:

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);

Methods

ackMsg

(
  • recipientId
  • messageId
)
protected

Internal method to take actions on reception of ack, for example, calling onDelivery handlers.

Parameters:

  • recipientId String

    The id of the party sending the acknowledgement

  • messageId String

    the 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 Object

    An object containing a set of listeners for various actions

    • [onIncomingMessage] Function optional

      Callback for incoming message

    • [onMessageDelivered] Function optional

      Callback 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
)
protected

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:

Returns:

Boolean defining if ack should be sent

newMessage

(
  • recipientIds
  • textBody
  • [publicHeaders]
)
Message

Removes a previous added eventListener object.

Note: Do not send the same Message object more than once.

Parameters:

  • recipientIds String | Array

    Array or string containing one or more recipientId's to send a message to.

  • textBody String

    The message body to send

  • [publicHeaders] Object optional

    Opaque object sent with the message which can be inspected on the receiver side via Message.headers

Returns:

Message:

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 Object

    An 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]
)
chainable async

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 Message

    The message object to send

  • [success=console.info] Function optional

    Callback for successfuly sent message (n.b. not necessarily delivered yet)

  • [fail=console.error] Function optional

    Callback 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.
    })