Deal with websockets

Introduction

HAPROXY

here we keep HTTP for websocket initialization then point to the NodeJS apps directly for the websocket sessions

frontend https-in
        ...

        # vhosts
        acl host_checkwss   hdr(host) -i checkwss.nethence.com

        # wss check
        acl is_upgrade          hdr(Connection) -i upgrade
        acl is_websocket        hdr(Upgrade)    -i websocket

        use_backend web if host_checkwss !is_upgrade !is_websocket
        use_backend wss if host_checkwss is_upgrade is_websocket

backend web
        balance roundrobin
        server web1 web1:80 check
        server web2 web2:80 check
        server web3 web3:80 check

backend wss
        balance roundrobin
        server web1wss web1:4000 cookie web1wss
        server web2wss web2:4000 cookie web2wss
        server web3wss web3:4000 cookie web3wss

you could also point to the same port and let NGINX do the job, as follows.

NGINX

    # wss - beware NO TRAILING-SLASH
    location / {
        proxy_pass http://127.0.0.1:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

Acceptance

wscat -c wss://checkwss.nethence.com/

check1

wscat -c wss://checkwss.nethence.com/

check2

wscat -c wss://checkwss.nethence.com/

check3

wscat -c wss://checkwss.nethence.com/

check4

Resources

WebSocket https://en.wikipedia.org/wiki/WebSocket

The WebSocket Protocol https://datatracker.ietf.org/doc/html/rfc6455

haproxy

ws { auto | h1 | h2 } https://cbonte.github.io/haproxy-dconv/2.4/configuration.html#ws

Websockets Load Balancing with HAProxy https://www.haproxy.com/blog/websockets-load-balancing-with-haproxy/

Scaling and Securing WebSockets with HAProxy https://infohubblog.com/scaling-and-securing-websockets-with-haproxy.html

haproxy=2.4.3: Websockets do not work on http/2 capable backends #1364 https://github.com/haproxy/haproxy/issues/1364

Support for Websockets with HTTP/2 to the backend #162 https://github.com/haproxy/haproxy/issues/162

nginx

NGINX as a WebSocket Proxy https://www.nginx.com/blog/websocket-nginx/

How to Configure Nginx as Reverse Proxy for WebSocket https://www.tutorialspoint.com/how-to-configure-nginx-as-reverse-proxy-for-websocket

How to Configure NGINX to Proxy WebSockets https://www.serverlab.ca/tutorials/linux/web-servers-linux/how-to-configure-nginx-for-websockets/

sample code

WebSockets and Node.js - testing WS and SockJS by building a web app https://ably.com/blog/web-app-websockets-nodejs

Integration testing WebSocket server in Node.JS https://medium.com/@basavarajkn/integration-testing-websocket-server-in-node-js-2997d107414c

WebSocket with NodeJS – Quick Getting Started Guide https://www.piesocket.com/blog/nodejs-websocket/ –> the one I used

How to test node.js websocket server? https://stackoverflow.com/questions/21293311/how-to-test-node-js-websocket-server

http/2

How to force CURL to ask for http/1.1? Or maybe there’s another issue, not sure https://stackoverflow.com/questions/41894411/how-to-force-curl-to-ask-for-http-1-1-or-maybe-theres-another-issue-not-sure

acceptance

wscat https://github.com/websockets/wscat


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5 | CONTACT
Copyright © 2024 Pierre-Philipp Braun