We have successfully integrated salesforce live agent using rest api and couple of things are done need help on below points.

  • Get Session Id (Working As expected).
  • Chat Request Initiation (Working As expected and getting successful status code)
  • Sending customer messages to salesforce agent (Working as expected and messages are getting shown in saleforoce)
  • Getting Agent Message from Salesforce to show to customers (We need a help on this api)

Basis on my research i have collected few points and want to understand how can we implement

  1. The System/message api will get notified with events happening at Salesforce server side.-is it true? Or we need to call this api to get the message send by agent?

2 Some people says we need to call this api to get the message sent by agent to receive at client side.. If this is the case how frequently we should call because if agent has a sent a message on 3 seconds after started and if I'm doing polling on 33rd seconds i will be wasting 30 seconds to pass on the agent messages to customer which is bad experience.

Is there any way we can notified automatically when agents sends the message immediately so that i should need to wait to happen next polling from client side.

1 Answer 1


The Chat API uses a long polling loop. This means that when you initiate a request to the server, the server waits until a message is sent, at which point the client will receive it immediately. You do need to be aware that the client session may time out, so your code must be prepared to reconnect before the server timeout, or the session will be lost. This is in the documentation.

Message long polling notifies you of events that occur on the Chat server for your Chat session. When you start a request, all pending messages will be immediately delivered to your session. If there are no pending messages, the connection to the server will remain open. The connection will return messages continuously as they are received on the server.

If your session expires, you will receive a 200 (“OK”) response code and a resource that contains an array of the remaining messages. If no messages were received, you will receive a 204 (“No Content”) response code.

When you receive a 200 (“OK”) or 204 (“No Content”) response code, immediately perform another Messages request to continue to retrieve messages that are registered on the Chat server.

⚠️ Warning

If you don’t make another Messages request to continue the messaging loop, your session will end after a system timeout on the Chat server.

If you don’t receive a response within the number of seconds indicated by the clientPollTimeout property in your SessionId request, your network connection to the server is likely experiencing an error, so you should terminate the request.

To initiate a long polling loop, perform a Messages request.

Long polling doesn't involve calling the server every few seconds to see if there's any new data. The connection waits until there is new data, and then you go again. In pseudo-code, that means your code looks like this:

do {
  // await means we're waiting for a response back
  let response = await callMessagesApi();
  /* When we get here, we have a response. This might be seconds 
   * or even minutes after the previous line of code started
   * executing.
  if(response.status != 200 && response.status != 204) {
    break; // We didn't get a valid response
  // Do stuff with the new messages here
} while(true); // Loop forever here, the loop breaks from within.
  • Thanks for this sfdcfox..i have very basic understanding of this long pooling.. I have gone through this document but it has not mentioned how it works actually.. The main concern i was trying to get it solved is How frequently should i call this system/messages api to check whether there any new messages or not. Doing similar kind of calling would be difficult if we are handling multiple chats from client side. If i need to call to check whether there are any messages in this case the communication between agent and customer goes ideal state for more time..
    – user8102
    Apr 22 at 18:09
  • @Bujji I edited in some more information. Does that clarify?
    – sfdcfox
    Apr 22 at 18:25
  • thank you so much for detailing the solutions it looks like my problem would solve by this approach. Let me try out this one and update you if need any further clarification👍
    – user8102
    Apr 22 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy