Back to articles
Technology Insight

Hosting a WebSocket Server for Real-Time Applications

May 6, 2026
Hosting WebSocket Server for Real-time Applications on VPS

Hosting WebSocket Server for Real-time Applications: Optimizing Socket.io & Phoenix Channels for C10k

Building real-time features such as live chat, push notifications, live dashboards, and collaborative tools has become essential for modern applications. WebSocket is the core technology that enables continuous two-way communication between server and client. This article provides a detailed guide on how to host a WebSocket Server on VPS, with optimized configuration to handle 10,000 concurrent connections (C10k problem) using Socket.io (Node.js) or Phoenix Channels (Elixir).

1. Why Optimize Your WebSocket Server?

WebSocket is fundamentally different from regular HTTP because each connection remains open and maintains state. Without proper optimization, the server can easily run out of RAM, CPU resources, or hit file descriptor limits, causing crashes or high latency.

  • C10k Problem: The ability to handle 10,000 concurrent connections is an important performance benchmark.
  • Real-world Applications: Chat apps, notification systems, real-time trading platforms, IoT dashboards.
  • Benefits of Optimization: Lower VPS costs, higher stability, and smoother user experience.

// Interface for managing WebSocket connections
interface ClientConnection {
  socketId: string;
  userId: string;
  room: string;
  connectedAt: Date;
  lastPing: Date;
  ip: string;
}

class RealtimeServer {
  private connections: Map = new Map();

  handleConnection(socket: any, userId: string) {
    const conn: ClientConnection = {
      socketId: socket.id,
      userId,
      room: "general",
      connectedAt: new Date(),
      lastPing: new Date(),
      ip: socket.handshake.address
    };
    
    this.connections.set(socket.id, conn);
    console.log(`[WebSocket] User ${userId} connected. Total connections: ${this.connections.size}`);
  }

  broadcastToRoom(room: string, event: string, data: any) {
    console.log(`[Broadcast] ${event} to room ${room} - ${this.connections.size} clients`);
  }
}
 

2. Recommended VPS Specifications for WebSocket

To reliably handle C10k+, your VPS needs strong CPU and RAM:

Concurrent Connections CPU Cores RAM Storage Recommended VPS Tier
1,000 - 5,000 4-6 cores 8-16GB NVMe 100GB Entry to Mid-tier High CPU
10,000+ 8-16 cores 32GB+ NVMe 200GB+ High CPU VPS

Choose a VPS with at least 1Gbps network port and locate it in a datacenter close to your users (Hanoi, Ho Chi Minh City, or Singapore).

3. Implementing Socket.io on Node.js

Socket.io is the most popular library thanks to its ease of use, fallback support, and rich feature set.


// server.ts - Optimized Socket.io Server
import { Server } from "socket.io";
import http from "http";

const httpServer = http.createServer();
const io = new Server(httpServer, {
  cors: { origin: "*" },
  pingTimeout: 60000,
  pingInterval: 25000,
  maxHttpBufferSize: 1e6 // 1MB
});

const activeUsers = new Map();

io.on("connection", (socket) => {
  console.log(`User connected: ${socket.id}`);

  socket.on("joinRoom", (room: string) => {
    socket.join(room);
    activeUsers.set(socket.id, { room });
  });

  socket.on("chatMessage", (data) => {
    io.to(data.room).emit("newMessage", data);
  });

  socket.on("disconnect", () => {
    activeUsers.delete(socket.id);
    console.log(`User disconnected. Total: ${activeUsers.size}`);
  });
});

httpServer.listen(3000, () => {
  console.log("Socket.io Server running on port 3000");
});
 

4. Phoenix Channels - High Performance Solution with Elixir

Phoenix Channels (Elixir) is famous for handling tens of thousands of connections on a single server thanks to its actor model and BEAM VM.


// Example logic equivalent to Phoenix Channel (illustrated in TypeScript)
interface ChannelMessage {
  topic: string;
  event: string;
  payload: any;
  ref?: string;
}

class PhoenixChannel {
  private subscribers: Map> = new Map();

  subscribe(topic: string, socketId: string) {
    if (!this.subscribers.has(topic)) {
      this.subscribers.set(topic, new Set());
    }
    this.subscribers.get(topic)!.add(socketId);
    console.log(`Subscribed to ${topic}. Total topics: ${this.subscribers.size}`);
  }

  broadcast(topic: string, event: string, payload: any) {
    const subs = this.subscribers.get(topic);
    if (subs) {
      console.log(`[Phoenix] Broadcast ${event} to ${subs.size} subscribers on ${topic}`);
    }
  }
}
 

5. Performance Optimization for C10k

  • Increase file descriptor limit (ulimit -n 65535).
  • Use Redis Adapter for Socket.io to enable horizontal scaling.
  • Enable HTTP/2 and TLS termination with Nginx.
  • Cluster Node.js using PM2 or run multiple instances.
  • Reduce payload size and compress messages.
  • Monitor with Prometheus + Grafana.

6. Nginx Configuration as Reverse Proxy


// Sample Nginx config for WebSocket support
const nginxConfig = `
upstream websocket_backend {
    server 127.0.0.1:3000;
}

server {
    listen 443 ssl;
    server_name realtime.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    location / {
        proxy_pass http://websocket_backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 86400;
    }
}
`;
console.log("Nginx has been configured for stable WebSocket support");
 

7. Conclusion: WebSocket Server Deployment Checklist

Before launching your real-time application into production, verify the following:

  1. Does your VPS have sufficient CPU, RAM, and network bandwidth?
  2. Have you optimized system limits (ulimit) and kernel parameters?
  3. Have you chosen Socket.io or Phoenix Channels based on your needs?
  4. Have you implemented Redis Adapter and clustering?
  5. Is Nginx + SSL properly configured for WebSocket?
  6. Do you have monitoring and alerts when connection count increases?

Effectively hosting a WebSocket Server will allow your chat and real-time notification applications to run smoothly with tens of thousands of users. This is a crucial skill for building competitive products in 2026.

Hope this detailed guide helps you successfully deploy a stable and powerful WebSocket Server on your VPS!