onNewMessage()

Registers a function to call when a new message is sent from a customer, agent, or supervisor. Available in API version 29.0 or later.

Syntax 

1sforce.console.chat.onNewMessage(chatKey:String, callback:Function)

Arguments 

NameTypeDescription
chatKeystringThe chatKey associated with the chat for which to call a function when a new customer message is received.
callbackfunctionJavaScript method called upon completion of the method.

Sample Code–Visualforce 

1<apex:page >
2    <apex:includeScript value="/support/console/68.0/integration.js"/>
3    <script type="text/javascript">
4        var eventHandler = function (result) {
5            alert('There is a new message in this chat: ' + result.content);
6        }
7        //Get the value for 'myChatKey'from the sforce.console.chat.getDetailsByPrimaryTabId() or other chat methods. 
8        //These values are for example purposes only
9        var chatKey = 'myChatKey';
10        sforce.console.chat.onNewMessage(chatKey, eventHandler);
11    </script>
12</apex:page>

Response 

This method is asynchronous so it returns its response in an object in a callback method. The response object contains the following properties:

NameTypeDescription
contentStringThe text of a message in the chat log.
nameStringThe name of the user who sent the message. This appears exactly as it is displayed in the chat log.
typeStringThe type of message that was received, such as an Agent or Visitor message.
timestampDate/TimeThe date and time the message was received.
successBooleantrue if firing event was successful; false if firing event wasn’t successful.