Node.js: socket.io path settings

You tested your socket.io connectionally on your local development environment, but after deploying to your remote server or production server, socket connection breaks. I think everyone has gone through this the first time. This is just a dirty example to counter the problem unless you configure your web server’s reverse proxy

Why separate paths matter When you run locally the Socket.IO endpoint usually sits at /socket.io, but in production your app is often mounted behind a gateway, load‑balancer or path prefix (for example /api-name). That means the browser must request /api-name/socket.io, otherwise the handshake requests (polling and websocket upgrade) go to the wrong path and will 404 or fail to upgrade. Separating path logic lets the client use the correct URL for each environment without changing reverse proxy configuration.

import { io, Socket } from "socket.io-client";

const isDev = process.env.NODE_ENV === "development";
const SOCKET_PATH = isDev ? "/socket.io" : "/api-endpoint/socket.io";
const SOCKET_HOST = isDev ? undefined : "https://api-endpoint.com";

const socket: Socket = io(SOCKET_HOST, {
  path: SOCKET_PATH,
  transports: ["websocket", "polling"],
});

export default socket;
  • We check process.env.NODE_ENV === “development”. That expression is false if NODE_ENV is undefined or any string other than “development”, so production or missing envs will use the production path.
  • SOCKET_PATH becomes “/socket.io” in development and “/api-endpoint/socket.io” otherwise.
  • SOCKET_HOST is left undefined for dev so the client connects to the same origin; in production you can set it to the real API origin if needed.
  • Keep the path consistent between client and server; no trailing slash; ensure CORS and websocket upgrade headers are allowed by your API host.

Open firewall for Expo node project (windows)

If you are testing your mobile app from a real devicc and need to access your expo app locally, here’s an example of opening the ports for the necessary services. (Windows powershell (admin))

# Allow Node.js inbound
New-NetFirewallRule -DisplayName "Node.js" -Direction Inbound -Program "C:\Program Files\nodejs\node.exe" -Action Allow

# Allow specific Expo port
New-NetFirewallRule -DisplayName "Expo Dev Server" -Direction Inbound -Protocol TCP -LocalPort 8081 -Action Allow

# Allow Metro bundler port range
New-NetFirewallRule -DisplayName "Metro Bundler" -Direction Inbound -Protocol TCP -LocalPort 19000-19006 -Action Allow

Node.js npx SecurityError: (:) [], PSSecurityException

PS C:\dev> npx
npx : File C:\Program Files\nodejs\npx.ps1 cannot be loaded because running scripts is disabled on this syste 
m. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.       
At line:1 char:1
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

If you can’t run the “npx” command on Windows, check your ExecutionPolicy via Windows PowerShell.

PS C:\> Get-ExecutionPolicy
Restricted
PS C:\> Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y
PS C:\> Get-ExecutionPolicy
RemoteSigned