server {
server_name example.com;
listen 3099 ssl;
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3099;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
ssl_certificate ...; # managed by Certbot
ssl_certificate_key ...; # managed by Certbot
}
const io = ...;
io.listen(3099);
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3099;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
const socket = io("wss://example.com:3099");
server {
server_name example.com;
location / {
proxy_pass http://localhost:3000;
}
location /peerjs {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location ~* \.io {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy false;
proxy_pass http://localhost:3000;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
listen 443 ssl;
ssl_certificate ...; # managed by Certbot
ssl_certificate_key ...; # managed by Certbot
}
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, {
transports: ["polling", "websocket"],
cors: {
origin: true,
methods: ["GET", "POST"],
credentials: false,
}
});
server.listen(3000);
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
<script>
const socket = io("wss://example.com", {
withCredentials: false,
reconnectionAttempts: 5,
})
</script>