The Node ws package accepts an HTTP agent in its connection options. For a forward proxy, an ES module fragment can use HttpsProxyAgent:

import WebSocket from 'ws';
import { HttpsProxyAgent } from 'https-proxy-agent';

const agent = new HttpsProxyAgent('http://proxy.example.net:8080');
const socket = new WebSocket('wss://socket.example.net/events', {
  agent,
});

socket.on('error', error => {
  console.error(error.message);
});

This assumes both packages are already part of the application. The WebSocket URL selects the service; the agent URL selects the proxy. An HTTP address from the HTTP proxy list can fill the agent URL for an allowed public WSS test without proxy credentials; check access to the actual service endpoint.

HttpsProxyAgent establishes an HTTP CONNECT tunnel and hands the connected transport to the client. It supports WebSocket connections as well as HTTPS requests, so the application does not need to construct the CONNECT exchange itself.

Continue with the application's ordinary open, message and close handling. The open event marks completion of the WebSocket handshake on the selected route; creating the JavaScript object alone has not completed it.

Mikanda mikatakata