Deploy - potatoscript/websocket GitHub Wiki
🚀 Deploying a WebSocket Server: A Step-by-Step Guide
Now that you have built your WebSocket server, it's time to deploy it so users can access it globally! In this guide, we will cover:
✅ Choosing a hosting platform
✅ Setting up the server
✅ Configuring a reverse proxy
✅ Securing with SSL (HTTPS & WSS)
✅ Testing after deployment
🏗 1. Choosing a Hosting Platform
You need a server to deploy your WebSocket application. Here are some popular options:
Platform | Type | Best For | Cost |
---|---|---|---|
Heroku | PaaS | Quick deployment | Free (Limited) |
Railway.app | PaaS | Simple setup | Free (Limited) |
Render | PaaS | Automatic deployment | Free (Limited) |
DigitalOcean | VPS | Custom setups | $$ |
Linode | VPS | Custom setups | $$ |
AWS EC2 | Cloud | Large-scale apps | $$$ |
Google Cloud | Cloud | Enterprise apps | $$$ |
For this guide, we will deploy on Railway.app (easy) and AWS EC2 (advanced).
🚀 2. Deploying on Railway.app (Easy & Free)
✅ Step 1: Create a Railway Account
1️⃣ Go to Railway.app
2️⃣ Click Sign Up and log in with GitHub
3️⃣ Click New Project → Deploy from GitHub
4️⃣ Select your WebSocket repository
✅ Step 2: Configure and Deploy
1️⃣ Click Environment Variables and set:
PORT=8080
2️⃣ Click Deploy and wait for it to complete
3️⃣ Copy the deployed URL (e.g., wss://yourapp.up.railway.app
)
🎉 Your WebSocket server is live!
🖥 3. Deploying on AWS EC2 (Advanced)
✅ Step 1: Launch an EC2 Instance
1️⃣ Log in to AWS Console
2️⃣ Go to EC2 Dashboard → Launch Instance
3️⃣ Choose Ubuntu 22.04 (or any OS you prefer)
4️⃣ Select t2.micro (Free Tier)
5️⃣ Configure Security Group:
- Allow Inbound Rules:
PORT 22 (SSH)
→ Your IPPORT 8080 (WebSocket)
→ Anywhere
6️⃣ Click Launch
✅ Step 2: Connect to Your Server
1️⃣ Open a terminal
2️⃣ Run the following command:
ssh -i your-key.pem ubuntu@your-ec2-ip
✅ Step 3: Install Node.js and WebSocket Server
Run these commands:
sudo apt update && sudo apt install -y nodejs npm
mkdir websocket-server && cd websocket-server
npm init -y
npm install ws express
✅ Step 4: Create a WebSocket Server
Create a file server.js
:
const WebSocket = require("ws");
const server = new WebSocket.Server({ port: 8080 });
server.on("connection", (socket) => {
console.log("🔗 Client connected!");
socket.on("message", (message) => {
console.log("📩 Received:", message);
socket.send(`Echo: ${message}`);
});
socket.on("close", () => console.log("🔴 Client disconnected"));
});
console.log("🚀 WebSocket server running on ws://localhost:8080");
✅ Step 5: Run the Server
node server.js
✅ Step 6: Keep It Running with PM2
Install PM2:
npm install -g pm2
pm2 start server.js
pm2 save
pm2 startup
🌍 4. Setting Up a Reverse Proxy with Nginx
To access your WebSocket server via a domain (e.g., wss://yourdomain.com
), you need a reverse proxy.
✅ Step 1: Install Nginx
sudo apt update
sudo apt install -y nginx
✅ Step 2: Configure Nginx for WebSockets
Edit Nginx config:
sudo nano /etc/nginx/sites-available/websocket
Add this configuration:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
Save and exit.
✅ Step 3: Enable the Config & Restart Nginx
sudo ln -s /etc/nginx/sites-available/websocket /etc/nginx/sites-enabled/
sudo systemctl restart nginx
🎉 Now your WebSocket server is accessible at ws://yourdomain.com
!
🔐 5. Securing with SSL (HTTPS & WSS)
To use wss://
(secure WebSockets), install Certbot SSL:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com
Now your WebSocket server is accessible at:
wss://yourdomain.com
🛠 6. Testing Your Deployed WebSocket Server
✅ Test with WebSocket Client
Open the browser console and run:
const socket = new WebSocket("wss://yourdomain.com");
socket.onopen = () => console.log("✅ Connected to server");
socket.onmessage = (event) => console.log("📩 Server says:", event.data);
socket.onclose = () => console.log("🔴 Disconnected");
✅ Test with WebSocket CLI (Linux/Mac)
wscat -c wss://yourdomain.com
🎯 7. Summary
✅ Railway.app for quick and free deployment
✅ AWS EC2 for full control
✅ Nginx for a reverse proxy
✅ Certbot SSL for secure WebSockets (wss://
)
✅ PM2 for running the server in the background