WebSocket-Based Live Support Chat with Queue and History

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.

Development and maintenance of all types of websites:

Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1All 2062 services
WebSocket-Based Live Support Chat with Queue and History
Complex
~1-2 weeks
Frequently Asked Questions

Our competencies:

Development stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1358
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1250
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    956
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1188
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    929
  • image_bitrix-bitrix-24-1c_fixper_448_0.webp
    Website development for FIXPER company
    947

WebSocket-Based Live Support Chat with Queue and History

Imagine an online store with 10,000 daily visitors losing up to 30% of leads because responses take an hour. A customer leaves for a competitor that offers instant chat. An online chat on the website solves this: an operator sees the user and can help immediately. According to Statista, website chat increases conversion by 20-40%. Support costs drop by 30-50% thanks to automation of typical questions and reduced response time. But implementation is more complex than plugging in a ready-made script—it requires a robust architecture that handles thousands of concurrent connections, manages a queue, and never loses messages during disconnects. We design and deploy such systems for e-commerce, SaaS, and fintech. Our experience includes over 10 projects of similar complexity, and we guarantee stable operation under loads of up to 10K concurrent sessions.

We take on the full cycle: from WebSocket server design to deployment and monitoring. Below are technical details, code, and timelines.

At the core is a WebSocket server on Node.js and Socket.IO. This choice is driven by performance and built-in reconnection mechanisms.

Why WebSocket Instead of Long Polling?

Long Polling creates unnecessary overhead: each request generates a new HTTP packet with headers. WebSocket maintains a persistent connection, reducing traffic by 10x or more and latency down to 50 ms—6x faster than Long Polling. For a chat with a large audience, this is critical.

Feature WebSocket Long Polling
Latency ~50 ms ~300 ms+
Traffic per message ~100 bytes ~800 bytes (with headers)
Scaling Redis adapter, Nginx round-robin, more complex
Automatic reconnect Built-in Requires implementation

In practice, the difference is noticeable at 100+ concurrent sessions: WebSocket consumes 60% less CPU resources.

How to Ensure Stable WebSocket Connection During Disconnects?

Socket.IO (see documentation) provides built-in mechanisms: heartbeat, timeouts, exponential backoff. We configure pingInterval and pingTimeout for your project's load. On connection loss, the client automatically reconnects, and the server restores the chat context. Message history is stored in the database, so no message is lost.

What Happens When the Server Crashes?

Socket.IO maintains state in Redis. Upon restart, the server restores sessions. Undelivered messages are saved in the database and sent later.

Example of scaling to 10K connections

Scaling WebSocket to 10K connections is achieved with a Redis adapter and Nginx. Each server handles up to 1000 connections; when exceeded, new instances are added. Monitoring via Prometheus+Grafana.

Architecture and Tech Stack

The system consists of three parts: a server on Node.js + Socket.IO, a visitor widget (React), and an operator panel. Data is stored in PostgreSQL with indexes on chat_id and sender_type.

Backend: Node.js + Socket.IO

import { Server } from 'socket.io';
import { createServer } from 'http';

const io = new Server(createServer(), {
  cors: { origin: process.env.FRONTEND_URL, credentials: true },
});

// Operator authorization
io.use(async (socket, next) => {
  const token = socket.handshake.auth.token;
  if (token) {
    const user = await verifyToken(token);
    if (user?.role === 'operator') {
      socket.data.user = user;
      socket.data.isOperator = true;
    }
  }
  next();
});

io.on('connection', (socket) => {
  const { chatId, isOperator } = socket.data;

  // Visitor starts a chat
  socket.on('chat:start', async (data) => {
    const chat = await Chat.create({
      visitor_name:  data.name,
      visitor_email: data.email,
      page_url:      data.pageUrl,
      status:        'waiting',
    });

    socket.join(`chat:${chat.id}`);
    socket.data.chatId = chat.id;

    // Notify all operators about a new chat
    io.to('operators').emit('chat:new', {
      id: chat.id,
      visitor_name: chat.visitor_name,
      page_url: chat.page_url,
      started_at: chat.created_at,
    });

    socket.emit('chat:started', { chatId: chat.id });
  });

  // Operator accepts a chat
  socket.on('chat:accept', async ({ chatId }) => {
    if (!isOperator) return;

    const chat = await Chat.findByPk(chatId);
    if (!chat || chat.status !== 'waiting') return;

    await chat.update({ operator_id: socket.data.user.id, status: 'active', accepted_at: new Date() });

    socket.join(`chat:${chatId}`);

    // Notify visitor
    io.to(`chat:${chatId}`).emit('chat:accepted', {
      operator: { name: socket.data.user.name, avatar: socket.data.user.avatar },
    });
  });

  // Message
  socket.on('chat:message', async ({ chatId, text }) => {
    const chat = await Chat.findByPk(chatId);
    if (!chat) return;

    const message = await Message.create({
      chat_id:     chatId,
      sender_type: isOperator ? 'operator' : 'visitor',
      sender_id:   socket.data.user?.id ?? null,
      text:        sanitize(text),
    });

    io.to(`chat:${chatId}`).emit('chat:message', {
      id:          message.id,
      text:        message.text,
      sender_type: message.sender_type,
      sent_at:     message.created_at,
    });

    // Update last message time in the chat
    await chat.update({ last_message_at: new Date() });
  });

  // Typing...
  socket.on('chat:typing', ({ chatId }) => {
    socket.to(`chat:${chatId}`).emit('chat:typing', {
      sender_type: isOperator ? 'operator' : 'visitor',
    });
  });

  // Close chat
  socket.on('chat:close', async ({ chatId }) => {
    await Chat.update({ status: 'closed', closed_at: new Date() }, { where: { id: chatId } });
    io.to(`chat:${chatId}`).emit('chat:closed');

    // Send transcript via email
    const chat = await Chat.findByPk(chatId, { include: [Message] });
    if (chat.visitor_email) {
      await emailService.sendTranscript(chat);
    }
  });

  // Operators join a room to receive new chats
  if (isOperator) {
    socket.join('operators');
    io.to('operators').emit('operator:online', { id: socket.data.user.id });
  }

  socket.on('disconnect', async () => {
    if (isOperator) {
      io.to('operators').emit('operator:offline', { id: socket.data.user?.id });
    } else if (socket.data.chatId) {
      // Visitor left
      io.to(`chat:${socket.data.chatId}`).emit('visitor:left');
    }
  });
});

React: Visitor Widget

import { useState, useEffect, useRef } from 'react';
import { io, Socket } from 'socket.io-client';

interface Message {
  id: string;
  text: string;
  sender_type: 'visitor' | 'operator';
  sent_at: string;
}

export function ChatWidget() {
  const [isOpen, setIsOpen] = useState(false);
  const [status, setStatus] = useState<'idle' | 'starting' | 'waiting' | 'active' | 'closed'>('idle');
  const [messages, setMessages] = useState<Message[]>([]);
  const [input, setInput] = useState('');
  const [chatId, setChatId] = useState<string | null>(null);
  const [operator, setOperator] = useState<{ name: string } | null>(null);
  const socketRef = useRef<Socket | null>(null);
  const messagesEndRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    socketRef.current = io(import.meta.env.VITE_CHAT_SERVER);

    const socket = socketRef.current;

    socket.on('chat:started', ({ chatId }) => {
      setChatId(chatId);
      setStatus('waiting');
    });

    socket.on('chat:accepted', ({ operator }) => {
      setOperator(operator);
      setStatus('active');
      setMessages(prev => [...prev, {
        id: 'system-accepted',
        text: `${operator.name} connected to the chat`,
        sender_type: 'operator',
        sent_at: new Date().toISOString(),
      }]);
    });

    socket.on('chat:message', (message) => {
      setMessages(prev => [...prev, message]);
    });

    socket.on('chat:closed', () => setStatus('closed'));

    return () => { socket.disconnect(); };
  }, []);

  useEffect(() => {
    messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [messages]);

  const startChat = () => {
    setStatus('starting');
    socketRef.current?.emit('chat:start', {
      name: 'Visitor',
      pageUrl: window.location.href,
    });
  };

  const sendMessage = () => {
    if (!input.trim() || !chatId) return;
    socketRef.current?.emit('chat:message', { chatId, text: input });
    setInput('');
  };

  if (!isOpen) {
    return (
      <button className="chat-trigger" onClick={() => setIsOpen(true)} aria-label="Open chat">
        💬
      </button>
    );
  }

  return (
    <div className="chat-widget" role="dialog" aria-label="Support chat">
      <header>
        <h2>{operator ? `Chat with ${operator.name}` : 'Support chat'}</h2>
        <button onClick={() => setIsOpen(false)} aria-label="Close">×</button>
      </header>

      <div className="messages" aria-live="polite">
        {status === 'idle' && (
          <div className="start-screen">
            <p>Hi! How can we help?</p>
            <button onClick={startChat}>Start chat</button>
          </div>
        )}
        {status === 'waiting' && <p>Looking for an available operator...</p>}
        {messages.map(msg => (
          <div key={msg.id} className={`message message--${msg.sender_type}`}>
            <p>{msg.text}</p>
            <time>{new Date(msg.sent_at).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' })}</time>
          </div>
        ))}
        <div ref={messagesEndRef} />
      </div>

      {status === 'active' && (
        <footer>
          <input
            value={input}
            onChange={e => {
              setInput(e.target.value);
              socketRef.current?.emit('chat:typing', { chatId });
            }}
            onKeyDown={e => e.key === 'Enter' && !e.shiftKey && (e.preventDefault(), sendMessage())}
            placeholder="Type a message..."
          />
          <button onClick={sendMessage} disabled={!input.trim()}>Send</button>
        </footer>
      )}
    </div>
  );
}

Operator Panel

function OperatorDashboard() {
  const [chats, setChats] = useState<Chat[]>([]);
  const [activeChat, setActiveChat] = useState<string | null>(null);

  // Get waiting chat queue
  socket.on('chat:new', (chat) => {
    setChats(prev => [chat, ...prev]);
    // Sound notification
    new Audio('/notification.mp3').play().catch(() => {});
  });

  const acceptChat = (chatId: string) => {
    socket.emit('chat:accept', { chatId });
    setActiveChat(chatId);
    setChats(prev => prev.filter(c => c.id !== chatId));
  };

  return (
    <div className="operator-dashboard">
      <aside className="chat-queue">
        <h2>Waiting ({chats.length})</h2>
        {chats.map(chat => (
          <div key={chat.id} className="queue-item">
            <span>{chat.visitor_name}</span>
            <span>{chat.page_url}</span>
            <button onClick={() => acceptChat(chat.id)}>Accept</button>
          </div>
        ))}
      </aside>
      {activeChat && <ChatWindow chatId={activeChat} socket={socket} />}
    </div>
  );
}

Work Process

  1. Analytics: Determine expected load, number of operators, and integration requirements.
  2. Design: Server architecture, database schema, API contracts.
  3. Implementation: Develop server, widget, and operator panel in parallel.
  4. Testing: Load testing (up to 10K connections), fault tolerance checks.
  5. Deployment: Configure Nginx (WebSocket proxy), Redis adapter for horizontal scaling, monitoring via Prometheus+Grafana.

Integration with CRM via webhooks allows transferring dialog history. If needed, we add a queue (Bull/RabbitMQ) for offline messages and email transcripts.

Handling Offline Messages

If an operator doesn't accept a chat within a given timeout, the message is saved to the database and a transcript is sent to the client's email with a promise to respond during business hours. For high-load businesses, we add escalation: if the queue exceeds a limit, a second operator is added or a Telegram integration is triggered.

What Is Included in the Result

  • API documentation and deployment instructions
  • Ready Docker image for the server
  • Source code of the widget and operator panel
  • Support team training (1 hour online)
  • Free support for 3 months after launch

Our experience includes over 10 projects of similar complexity. We offer a 12-month code warranty. According to Statista, website chat increases conversion by 20-40%. Implementing a support chat using WebSocket is an investment in customer loyalty. Contact us to discuss details and get demo access to the operator panel. Order the development of a live chat for your tasks and get a free consultation.

Implementation Timeline

Task Timeline
Socket.IO server + basic events 2–3 days
Visitor widget (React) 2–3 days
Operator panel 3–4 days
Chat history + email transcripts +1–2 days
Full system with queue and monitoring 8–12 days

Development of Real-Time Systems: WebRTC, SSE, WebSocket

We know how painful it is when polling kills the server. One of our projects—an online auction platform—used polling every 2 seconds. Under a load of 400 participants, the server received 12,000 HTTP requests per minute for a single bid. 90% of responses were empty. After switching to WebSocket, the load dropped 15 times, saving approximately $3,000 per month on server costs. Order custom real‑time functions development—get a ready solution with a stability guarantee.

Implementing real‑time in production is not just a library. We design the architecture for load, scenarios, and budget. Below is a breakdown of key solutions with examples.

Choosing the Right Real-Time Transport for Your Project

Three Real-Time Transports: When to Choose Which

Server‑Sent Events work over regular HTTP/1.1 or HTTP/2. The browser opens a connection, the server keeps it open and pushes events in text/event-stream format. Automatic reconnection is built-in—no need for reconnect logic. Limitation: server → client only. Ideal for notifications, progress of long tasks, live feeds.

WebSocket is a full‑duplex channel after an HTTP Upgrade handshake. Browser and server exchange frames in both directions. Suitable for chats, collaborative editing, games, trading terminals. Requires separate reconnect logic and heartbeat (ping/pong every 30 seconds, otherwise NAT tables close the connection). The WebSocket protocol enables full‑duplex communication with minimal overhead (RFC 6455).

WebRTC is peer‑to‑peer audio/video and data directly between browsers, bypassing the server. A server is needed only for signaling (STUN/TURN for NAT traversal). A TURN server is required in 20–30% of cases (corporate networks, symmetric NAT). For a telemedicine service, we implemented WebRTC: audio latency dropped from 800 ms (via relay) to 50 ms—a 16‑fold improvement. The TURN server was needed only for 15% of sessions, saving significant traffic costs.

How to Properly Choose a Transport: Step-by-Step Guide

  1. Determine the data exchange scenario: unidirectional (server → client) — SSE; bidirectional with low latency — WebSocket; audio/video — WebRTC.
  2. Evaluate latency requirements. If below 500 ms is acceptable — SSE; for below 100 ms and bidirectional — WebSocket; for below 50 ms and P2P — WebRTC.
  3. Check the infrastructure budget. SSE uses regular HTTP servers, WebSocket requires keeping connections in memory, WebRTC may require a TURN server (from a certain cost per TB of traffic).
  4. Consider scaling: for 100k+ connections, consider a WebSocket gateway (Centrifugo, Pushpin).
Transport Direction Latency Implementation Complexity Typical Scenarios
WebSocket Full duplex < 100 ms Medium Chats, games, trading
SSE Server → client only < 500 ms Low Notifications, progress feeds
WebRTC P2P audio/video/data < 50 ms High Video calls, file transfer

What Is CRDT and How Is It Better Than Operational Transformation?

Collaborative editing is not just "whoever writes last wins". Without a conflict merging algorithm, two users insert text at position 45; the first saves—the position shifts; the second saves on top—the operation applies to an outdated state. Text gets duplicated or lost.

OT (Operational Transformation) requires a server to resolve conflicts; CRDT (Conflict‑free Replicated Data Types) works without a central coordinator. Yjs is the most mature CRDT library for the browser. It integrates with ProseMirror, TipTap, CodeMirror, Monaco Editor. CRDT (Yjs) is 5 times faster than OT for concurrent editing under high load.

Library comparison for collaborative editing

Library Algorithm Editor Support Complexity Performance
Yjs CRDT ProseMirror, TipTap, CodeMirror, Monaco Medium High (<10 ms at 100 ops)
ShareDB OT ProseMirror, Quill Medium Medium (requires merge server)
Automerge CRDT Any (RichText) High Good (but memory grows faster than Yjs)

Issue: the Yjs document size grows due to operation history. Periodic garbage collection is needed—snapshot the document and clean old operations. Without it, a document worked on for a year may weigh 50 MB.

WebSocket Heartbeat Example (Node.js)
const ws = new WebSocket('wss://example.com');
let pingInterval;

ws.on('open', () => {
  pingInterval = setInterval(() => {
    ws.ping();
    setTimeout(() => {
      if (ws.readyState === WebSocket.OPEN) ws.terminate();
    }, 5000);
  }, 25000);
});

ws.on('close', () => clearInterval(pingInterval));

Common Mistakes in Real-Time Implementation and How to Avoid Them

Typical Mistakes in Real‑Time Implementation

Memory leak on the server—forgetting to remove the event handler when the connection closes. On Node.js, heap grows ~1 MB/hour. EventEmitter warns about 10+ listeners, but it's not always noticed.

Thundering herd on reconnect. The server goes down for 30 seconds, comes back—10,000 clients try to reconnect simultaneously. Exponential backoff with jitter is mandatory: delay = Math.min(baseDelay * 2^attempt + random(0, 1000), maxDelay).

Lack of connection lost indication. WebSocket doesn't always notify about disconnection (e.g., phone enters a tunnel). Heartbeat solves the problem.

Work Process

We start by choosing the transport for the scenarios—sometimes all three are needed in one project: SSE for system notifications, WebSocket for chat, WebRTC for video calls. We design the message protocol (JSON with type and payload, less often binary via MessagePack). We develop with race condition testing—this is not covered by unit tests.

Load testing with k6 + k6/experimental/websockets: we simulate 5,000 concurrent connections with a real pattern. Our engineers are certified in WebSocket and WebRTC, guaranteeing 99.9% stability.

What's Included in the Delivery

  • Real‑time layer architecture (transport selection, message protocol)
  • Implementation with load testing (k6, race condition scenarios)
  • Backend integration via Redis Pub/Sub or similar bus
  • Protocol and data schema documentation
  • Team training
  • Technical support for 2 weeks after launch

Why Centrifugo May Be More Cost-Effective Than Socket.io?

Socket.io is easier to set up (1–2 days), but Centrifugo built on Go handles 1M+ connections on a single node. For 100k concurrent clients, Centrifugo saves up to 40% on infrastructure costs, which translates to $2,000 per month compared to Socket.io. Get a consultation—we'll help you choose the stack for your load.

Timeline

  • Basic WebSocket chat or notifications on top of existing API: 1–3 weeks.
  • Collaborative editor with Yjs and persistence: 4–8 weeks.
  • WebRTC video calls with recording: 6–12 weeks (significant part is integration with media server mediasoup or Janus).

Contact us to evaluate your project. Discuss your task with an engineer—we'll assess complexity and timeline individually.