WebSocket API

Use the WebSocket API for real-time live chat integration. It provides instant message delivery, typing indicators, presence detection, auto-translation, and session management.

Endpoint

wss://webchatagent.com/api/livechat

The WebSocket API uses JSON messages for all communication.

Connection Roles

When connecting, you specify your role:

RoleDescription
userA website visitor participating in a chat
adminA human agent managing live chat sessions
widgetThe embedded chat widget on a website

Client Events (You Send)

client:join

Join a live chat session. This is the first event you must send after connecting.

{
  "type": "client:join",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "as": "user",
  "lang": "en",
  "translateEnabled": true,
  "retranslateBacklog": false
}
FieldTypeRequiredDescription
chatbotIdstringYesYour chatbot's UUID.
sessionIdstringYesThe chat session ID.
asstringYesConnection role: user, admin, or widget.
langstringNoPreferred language code (e.g. en, de, fr).
translateEnabledbooleanNoEnable auto-translation for this session.
retranslateBacklogbooleanNoTranslate existing messages when joining.

client:message

Send a message in the current session.

{
  "type": "client:message",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "sender": "user",
  "content": "I need help with my order",
  "lang": "en"
}
FieldTypeRequiredDescription
chatbotIdstringYesYour chatbot's UUID.
sessionIdstringYesThe chat session ID.
senderstringYesWho is sending: user or admin.
contentstringYesThe message text.
langstringNoMessage language for translation.

client:typing

Send typing indicator.

{
  "type": "client:typing",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "sender": "user",
  "isTyping": true
}

client:meta

Update session metadata (page information).

{
  "type": "client:meta",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "pageUrl": "https://example.com/pricing",
  "pageTitle": "Pricing - Example",
  "referrer": "https://google.com",
  "browserLanguage": "en-US"
}

client:agent_takeover

Admin takes over a session from the bot.

{
  "type": "client:agent_takeover",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID"
}

client:end

End a live chat session.

{
  "type": "client:end",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "reason": "resolved"
}

client:widget_open / client:widget_close

Notify when the chat widget is opened or closed.

{
  "type": "client:widget_open",
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "pageUrl": "https://example.com",
  "pageTitle": "Home",
  "browserLanguage": "en-US"
}

Server Events (You Receive)

livechat:status

Session status update.

{
  "type": "livechat:status",
  "ok": true,
  "chatbotId": "YOUR_CHATBOT_ID",
  "sessionId": "SESSION_ID",
  "status": "open",
  "mode": "human",
  "userName": "John",
  "userEmail": "john@example.com",
  "userLanguage": "en"
}
FieldTypeDescription
statusstringSession status: waiting (waiting for agent), open (agent connected), closed.
modestringCurrent mode: bot or human.
userNamestringVisitor's name (if collected via Leads).
userEmailstringVisitor's email (if collected).
userLanguagestringDetected visitor language.

livechat:message

New message in the session.

{
  "type": "livechat:message",
  "id": "msg_abc123",
  "sessionId": "SESSION_ID",
  "sender": "admin",
  "content": "Hello! How can I help you today?",
  "country": "DE",
  "createdAt": "2025-01-15T10:30:00Z",
  "file": null,
  "audience": null,
  "historical": false
}
FieldTypeDescription
idstringUnique message ID.
senderstringMessage sender: user, admin, or assistant.
contentstringMessage text content.
countrystringSender's country code (if detected).
createdAtstringISO 8601 timestamp.
fileobjectAttached file information (if any).
audiencestringMessage visibility scope (if restricted).
historicalbooleanWhether this is a backlog message sent on join.

livechat:typing

Typing indicator from another participant.

livechat:presence

Online/offline status of participants.

livechat:meta

Updated session metadata (current page, referrer, etc.).

livechat:ended

Session has been ended.

{
  "type": "livechat:ended",
  "sessionId": "SESSION_ID",
  "reason": "resolved"
}

livechat:active_count / livechat:waiting_count

Current count of active and waiting sessions (admin only).

livechat:new_request

Notification of a new live chat request (admin only).

livechat:widget_sessions

List of currently active widget sessions (admin only).

Auto-Translation

The WebSocket API supports automatic translation between visitor and agent languages:

  • Set translateEnabled: true when joining a session
  • Set lang to your preferred language
  • Messages from the other party are automatically translated using Gemini AI
  • Original and translated text are both available

This allows agents to communicate with visitors in any language without manual translation.

Example: Custom Live Chat Client

// Connect to WebSocket
const ws = new WebSocket('wss://webchatagent.com/api/livechat');

ws.onopen = () => {
  // Join a session as a user
  ws.send(JSON.stringify({
    type: 'client:join',
    chatbotId: 'YOUR_CHATBOT_ID',
    sessionId: 'SESSION_ID',
    as: 'user',
    lang: 'en'
  }));
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);

  switch (data.type) {
    case 'livechat:message':
      console.log(`${data.sender}: ${data.content}`);
      break;
    case 'livechat:typing':
      console.log('Agent is typing...');
      break;
    case 'livechat:status':
      console.log(`Session status: ${data.status}, mode: ${data.mode}`);
      break;
    case 'livechat:ended':
      console.log(`Session ended: ${data.reason}`);
      ws.close();
      break;
  }
};

// Send a message
function sendMessage(text) {
  ws.send(JSON.stringify({
    type: 'client:message',
    chatbotId: 'YOUR_CHATBOT_ID',
    sessionId: 'SESSION_ID',
    sender: 'user',
    content: text,
    lang: 'en'
  }));
}

// Send typing indicator
function sendTyping(isTyping) {
  ws.send(JSON.stringify({
    type: 'client:typing',
    chatbotId: 'YOUR_CHATBOT_ID',
    sessionId: 'SESSION_ID',
    sender: 'user',
    isTyping
  }));
}