Nodejs project connceting to another project

Is it possible to for a nodejs backend to connect to another nodejs backend in anoither project?

I tried using websockets to do the connection.
I followed the client and server examples here websocket - npm ( but it doesnt seem to work. The project that connects is getting a 502 error.

Connect Error: Error: Server responded with a non-101 status: 502 Bad Gateway
Response Headers Follow:
server: awselb/2.0
date: Tue, 04 Jul 2023 09:57:16 GMT
content-type: text/html
content-length: 122
connection: keep-alive

make sure the connection sends a user-agent header

1 Like

Still returning the error
This is my code, am I doing something wrong?

 var WebSocketClient = require('websocket').client;
     var sclient = new WebSocketClient();
     sclient.on('connectFailed', function(error) {
         console.log('Connect Error: ' + error.toString());
     sclient.on('connect', function(connection) {
         console.log('WebSocket Client Connected');
         connection.on('error', function(error) {
             console.log("Connection Error: " + error.toString());
         connection.on('close', function() {
             console.log('echo-protocol Connection Closed');
         connection.on('message', function(message) {
             if (message.type === 'utf8') {
                 console.log("Received: '" + message.utf8Data + "'");
     sclient.connect('ws://', 'echo-protocol' , {
  headers: {
    "User-Agent": "WebSocket Client"

hmm I used http post requests and it worked

looks like you’re wrapping the headers in an additional object. docs say you just pass headers… but the deep nested brackets are confusing. could you check?

Hmm still doesnt work
I think i’ll stick to http requests for now
Thanks for your help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.