A live chat widget works as a piece of JavaScript that your page loads from the chat vendor. The script fetches your settings, builds a chat button and window in a layer separate from your page, and exchanges messages with the vendor's server through web requests. It saves the conversation in the visitor's browser, so the chat continues on the next page.
Below, each stage is shown in the real code of one widget: Asyntai's.
It starts with one snippet
To embed a live chat, you paste a short script into your site's template. This is the whole Asyntai embed code. The only value that belongs to you is the widget ID.
<script>(function(){var l=function(){var s=document.createElement("script");s.src="https://widget.asyntai.com/static/js/chat-widget.js";s.async=true;s.setAttribute("data-asyntai-id","YOUR_WIDGET_ID");document.head.appendChild(s)};if(document.readyState==="complete"){l()}else{window.addEventListener("load",l)}})();</script>
The snippet waits until the browser has finished loading your page. If that has already happened, it continues at once. Your own content comes first.
It then creates a new script tag from JavaScript, so the widget file is requested only at this point.
This tells the browser to fetch the file without stopping other work on the page.
This is how the script finds out which website it belongs to. Colours, position and greeting are not in the snippet. They come from your dashboard, so changing them does not touch your site's code.
What the script does when it runs
The order matters, because each stage depends on the one before.
Read the ID
The script looks for its ID. Without one it writes an error to the browser console and stops, and the page stays as it was.
Ask the server
Two requests run side by side. One fetches your settings. The other asks whether this widget should show.
Check the page
It compares the address with your list of excluded pages. On a listed page, such as a checkout, the widget does not appear.
Build the interface
The script adds its styles and creates the button and window. Then it restores saved messages and attaches its event handlers.
On a second page of the same visit, the widget has a shortcut. It saved your settings in the browser last time, so it can draw the button from that copy and update it when the fresh settings arrive. That stops the button from disappearing and returning as a visitor clicks around.
Why your CSS does not break the chat window
A widget lives on someone else's page, with someone else's styles. A rule such as button { background: red; } in your stylesheet could change every button in the chat, and the widget's own styles could damage your page. Vendors solve this in one of two ways.
Iframe
- A separate document inside the page
- Very strong separation from your page
- Its size and position need extra management
- Your page's scripts cannot easily reach inside
Shadow DOM
- A protected branch of the same document
- Your stylesheet does not restyle what is inside
- Fixed positioning works directly against the screen
- The widget's styles stay inside the branch
The Asyntai widget uses shadow DOM. It adds a host element to the page and sets a very high stacking order on it, so it sits above your content. The host has no size and ignores clicks, so only the button and window inside it can catch the mouse. To change the look, you use the widget settings, because page CSS cannot reach inside.
What the widget keeps in the browser
A normal page change destroys the running script. The next page loads it again, so anything that must last has to be written down somewhere. Live chat widgets typically use browser storage, because the visitor's conversation has to return with them.
| Item | Why it is saved |
|---|---|
| Session ID | A random ID created on the first visit. It connects each message to one conversation on the server. |
| Session token | Issued by the server. It shows that this browser owns the conversation before the stored history is used. |
| Messages | A copy of the conversation, so the window can be redrawn on the next page. |
| Open or closed state | If the visitor left the chat open, it reopens on the next page. |
| Settings copy | Used to draw the button on the next page while fresh settings load. |
Each key includes the widget ID, so two widgets on one domain do not mix. If a privacy-focused browser blocks storage, the widget treats each page as a first visit and goes on working.
What happens when the visitor sends a message
The window is a normal form inside the shadow DOM. On submit, the script does the following:
- Shows the message. It adds the visitor's text to the window and clears the input.
- Shows a typing indicator, so the visitor can see that an answer is on the way.
- Posts the data. A web request carries the widget ID, the message, the session ID, the current page address and any signed-in user details your site passes.
- Retries on a failure. If the request fails, the script tries again up to three times in total. It does not retry when the server says the widget is blocked or a limit is reached.
- Shows the reply. It removes the indicator, adds the answer, and saves the conversation to storage.
Screen readers get a small detail of their own: a hidden status line announces each reply once it has finished arriving, so it is not read out in pieces.
Replies from a person take a different route, because nobody is holding a request open for them. While the window is open, the widget asks the server every few seconds whether your team has written anything. When a member of your team takes over, a feature on Standard and Pro, that same check brings the status change and shows a Live Agent indicator. Other vendors use a WebSocket for this job. The visitor sees the same result: a message appears in the window.
On phones
The same script runs on a phone, with some differences. While the chat is open it stops the page behind from scrolling, so a swipe moves the conversation and the page stays still. Some button designs drop their extra text on small screens. In the auto-trigger setting, Full Chat + Bubble opens the full chat on a desktop and shows only a notification bubble on a phone, because a window that opens by itself is more disruptive on a small screen.
The chat widget API: control it from your own code
Once loaded, the script adds an object to the page, and your own code can call it.
| Call | What it does |
|---|---|
window.AsyntaiWidget.open() | Opens the chat, for example from a button in your menu |
window.AsyntaiWidget.close() | Closes the chat |
window.AsyntaiWidget.toggle() | Opens it if closed, and closes it if open |
window.AsyntaiWidget.ask("Do you ship to Norway?") | Opens the chat and sends that question. See Ask AI buttons |
asyntai:ready | An event on window that fires when the widget is ready for these calls |
Calling open() before the widget is ready does nothing, so wait for that event if your code runs early. For data that goes beyond the browser, the REST API is available from Starter, and User Context and custom tools are available from Standard.
A live chat widget in React
A React live chat widget does not need a component. Put the snippet in public/index.html. The script then loads once with the page, outside your React tree. A route change does not reload the page, so the chat stays open. There is more on the React page, and Next.js has its own package.
Inside the page, in place of a floating button
A floating button is one layout. You can also place the chat in a section of a page with embedded chat, or use an iframe embed where a platform allows only an iframe.
When the widget does not appear
The stages above tell you where to look. Open the browser's developer tools and check the console.
- Missing ID. The console says that
data-asyntai-idis required. Copy the code again from your dashboard. - Blocked by a security policy. If your site sends a Content Security Policy, allow
widget.asyntai.com,asyntai.comandflagcdn.com. - An excluded page. Check the list in Exclude Pages.
- A cached page. A caching plugin can keep serving the page from before you added the code. Clear the cache and test in a private window.
- Hidden behind something. A sticky bar or cookie banner can cover the button. From Starter, you can move the button with exact offsets.
Frequently asked questions
Does a live chat widget slow down a website?
Any extra script is some work for the browser, so measure your own page. Run a test such as PageSpeed Insights before and after you install, on the same page. In the Asyntai snippet, the file is requested after your page has finished loading, with async set.
Does the code change when I change the widget's colours?
No. The script reads your settings from the server on each page, and the snippet holds only the ID.
Do I need a different code for WordPress or Shopify?
You can use the same code in the theme, or use a platform route such as the Shopify app or the WordPress plugin. The installation guide has steps for each platform.
What does the Asyntai widget cost?
The same widget runs on each plan: Free at $0 with 50 messages a month, Starter at $39 with 800, Standard at $139 with 15,000, and Pro at $449 with 50,000. One message is one AI reply. The pricing page shows what each plan adds.
Where does the AI get its answers?
The widget only carries the messages. The answers come from the server, which reads your pages and files. Our guide to how live chat works on a website covers that side.