WebSocket con server pubblico
After trying WebSockets locally, let’s see how to use a remote WebSocket server. By publishing server.js on a cloud service for persistent Node.js processes, such as Render, the server becomes reachable over the network and browsers can connect to its secure wss:// endpoint—even from workstations that cannot communicate directly over the local network or from outside the classroom.
Diagram created with Mermaid
Introduction
Section titled “Introduction”Server repository structure
Section titled “Server repository structure”The GitHub repository dedicated to the WebSocket server needs at least the essential files for installing dependencies and starting the process:
Directoryws-chat-server/
- package.json Configuration with
"type": "module"and thewsdependency - server.js WebSocket server listening on
process.env.PORT || 8080
- package.json Configuration with
Step-by-step procedure
Section titled “Step-by-step procedure”-
Adapt the port in
server.jsIn a cloud environment, the port is not fixed at
8080; it is assigned through theprocess.env.PORTenvironment variable:import { WebSocketServer, WebSocket } from 'ws';const PORT = process.env.PORT || 8080;const wss = new WebSocketServer({ port: PORT });console.log(`WebSocket server listening on port ${PORT}`); -
Publish
package.jsonandserver.jsto GitHub Verify thatpackage.jsoncontains"type": "module"andwsin"dependencies"(you do not need to uploadnode_modules), then commit and push it to a GitHub repository. -
Create a Web Service on Render Sign in to the Render dashboard, click New + → Web Service, and connect your GitHub repository. Configure:
- Build Command:
npm install - Start Command:
node server.js
- Build Command:
-
Start the deployment and copy the service URL Finish creating the service and wait for the deployment to become active. Render will assign a URL such as
https://your-service-name.onrender.com. -
Update the
wss://address inindex.htmlIn the client, replace the local address with the correspondingwss://endpoint (withouthttps://):const socket = new WebSocket('wss://your-service-name.onrender.com'); -
Open
index.htmlin browsers on several workstations in the lab and send a message to verify real-time broadcasting.
Summary
Section titled “Summary”Learning Check
Section titled “Learning Check”Quiz answers on this page are saved in your browser's local storage and remain saved between visits
Which change do we make to the port in server.js when moving from localhost to Render?
- Set a fixed port of
8080, ignoring the environment. - Use
const PORT = process.env.PORT || 8080;so Render assigns the port dynamically while retaining8080as a local fallback.Correct: on Render, the port is supplied through the
process.env.PORTenvironment variable. - Remove the
{ port: PORT }argument fromWebSocketServer.
In the index.html client, what changes in the address passed to new WebSocket(...) when moving from localhost to Render?
- It changes from
ws://localhost:...towss://<service-name>.onrender.com.Correct: the Render service exposes a secure TLS endpoint, so the client uses
wss://without a local port number. - Use
http://<service-name>.onrender.com:8080. - No URL change is needed because
localhostalready points to the Render service.
Which commands do we configure on the Render Web Service to install dependencies and start the server instead of starting it manually locally?
- Build Command:
node server.js— Start Command:npm install - Build Command:
npm run dev— Start Command:index.html - Build Command:
npm install— Start Command:node server.jsCorrect: Render installs the packages from
package.jsonwithnpm installand starts the server withnode server.js.
Quiz progress
0 / 0 answers to questions (0%)
0 correct answers
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0