Skip to content

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.

flowchart LR PC1["Lab PC 1 (Browser + index.html)"] -->|wss://...| Cloud["Render Web Service (server.js on process.env.PORT)"] PC2["Lab PC 2 (Browser + index.html)"] -->|wss://...| Cloud PC3["Lab PC 3 (Browser + index.html)"] -->|wss://...| Cloud Cloud -->|JSON broadcast| PC1 Cloud -->|JSON broadcast| PC2 Cloud -->|JSON broadcast| PC3

Diagram created with Mermaid

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 the ws dependency
    • server.js WebSocket server listening on process.env.PORT || 8080
  1. Adapt the port in server.js

    In a cloud environment, the port is not fixed at 8080; it is assigned through the process.env.PORT environment 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}`);
  2. Publish package.json and server.js to GitHub Verify that package.json contains "type": "module" and ws in "dependencies" (you do not need to upload node_modules), then commit and push it to a GitHub repository.

  3. 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
  4. 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.

  5. Update the wss:// address in index.html In the client, replace the local address with the corresponding wss:// endpoint (without https://):

    const socket = new WebSocket('wss://your-service-name.onrender.com');
  6. Open index.html in browsers on several workstations in the lab and send a message to verify real-time broadcasting.

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 retaining 8080 as a local fallback.

    Correct: on Render, the port is supplied through the process.env.PORT environment variable.

  • Remove the { port: PORT } argument from WebSocketServer.

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:... to wss://<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 localhost already 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.js

    Correct: Render installs the packages from package.json with npm install and starts the server with node server.js.

Quiz progress

0 / 0 answers to questions (0%)

0 correct answers