Mercure Live Chat

How to add a live chat to a website

creating a chat using the Mercure protocol
storing messages on the client side
Want the same?

About the project

We are currently working on a platform for reaching out to influencers and media to promote products, services, and personal brands. As the platform was developing, the client felt the need to add live chat functionality so that platform clients could communicate with its administrators and copywriters.

Chat requirements:

  • chat history should be stored on the client side;
  • administrators have to monitor the status of messages between clients and copywriters;
  • notifications must be sent within the platform or by email.

Taking all this into account, we decided not to use ready-made services. An example of a ready-made service is the paid version of JivoChat, where messages pass through a server. Instead, we have selected technologies that can be used to build an independent chat room.

add website chat

Does your website need a live chat?

The chat is used by platform users, copywriters, and admins. When the user provides all the necessary information (the text's message, volume, objective, audience, style, and tone of voice) and pays the fee, an individual page for the commission is created.

Additionally, the user can purchase search engine optimization, publication distribution, etc., depending on the selected service package.

In the chat, the client and the contractor discuss the job details. The platform administrator can also communicate with the client, but his main task is to monitor the quality of services provided by the contractor.

Chat development

To develop the live chat, we used the Mercure protocol, which utilizes the Server-Sent Events standard.

Mercure protocol

We used the Mercure protocol for the job. Its task is to send the content writers new chat message notifications or deliver the message without reloading the page if the commission page is currently open on the author's device.

Messages are received through our server, which saves them and passes the command to the Mercure hub. After that, Mercure notifies everyone following updates on a specific task.

mercure protocol


Mercure works with the Symfony framework through a ready-made open-source component and is an alternative to the WebSocket protocol. Unlike bidirectional WebSocket, Mercure only transfers data from the server to the client using the HTTP/2 protocol. It helps to use network resources more efficiently.

HOW WE DEVELOPED A CRM SYSTEM ON SYMFONY ➜

Server-Sent Events

Among several options for chat development, we opted for the Server-Sent Events (SSE) standard. As the name suggests, the point of the standard is to send events from the server. The browser connects to the website URL and makes a request to the server. The server keeps the request open and sends messages to the browser. The built-in JavaScript API called EventSource allows you to connect to the server and hold a request. Another convenient property of EventSource is that it automatically establishes a new connection if the old one suddenly breaks.

SSE is a simpler and cheaper alternative to WebSocket when the project does not need all the latter's capabilities, such as a bidirectional connection.

websocket or eventsource


We have set up message filtering, or so-called escaping: special characters (for example, a slash) are added before the lines, which prevents malicious code from running.

Result

Using Server-Sent Events and Mercure technologies, we have developed a live chat with the messages stored only on the client side. The data flow in this case is unidirectional, so using Mercure makes implementation inexpensive and painless.

Drop us a line

Your email isn’t going to end up in the inbox abyss, never replied.
At ADCI Solutions, we care about each inquiry

Next case study

Facade Manufacturer

Field mapping for Salesforce Drupal integration