Tag: WebSocket

  • Enable WebSocket in Cpanel Server with Nginx

    Enable WebSocket in Cpanel Server with Nginx

    On a Cpanel server, WebSocket server was running on port 8080. It worked fine with HTTP. After enabling HTTPS, it stopped working.

    The website code had the following entry

    var websocket_server = new WebSocket("ws://domain.com:8080");
    

    This Cpanel server had ea-nginx (Nginx provided by Cpanel) installed.

    To fix the error, I created a file

    vi /etc/nginx/conf.d/ws-domain.com.conf
    

    In the above, replace domain.com with the actual domain name.

    Add following content

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    
    upstream websocket {
        server 127.0.0.1:8080;
    }
    

    Create file

    vi /etc/nginx/conf.d/users/CPANEL_USER/domain.com/wss.conf
    

    Replace CPANEL_USER with the actual Cpanel user name for the website. domain.com with real domain name,

    Add following

    location /wsapp/ {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
    }
    

    Restart nginx

    systemctl restart nginx
    

    In your application code, find

    var websocket_server = new WebSocket("ws://domain.com:8080");
    

    Replace with

    var websocket_server = new WebSocket("wss://domain.com/wsapp/NNN");
    

    See Cpanel Server

  • WebSocket

    WebSocket

    WebSocket Server is a technology that enables real-time communication between a web application and a server. It provides a bidirectional data transmission mechanism that allows both the client and the server to send messages to each other simultaneously. Unlike traditional HTTP communication, WebSockets enable full-duplex communication between the client and server, making it ideal for real-time applications.

    A WebSocket Server works by establishing a persistent connection between the client and the server using the WebSocket Protocol. This connection remains open, allowing for two-way communication, unlike traditional HTTP communication, which is based on request-response interactions. The WebSocket Protocol allows for low latency and high-frequency data transfer, making it suitable for real-time applications.

    Enable WebSocket in Cpanel Server with Nginx

    To test a WebSocket server, use

    https://ws-playground.netlify.app/