question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ws not sending message to wss server

See original GitHub issue

I updated this drastically becasue I have a new approach but am still getting errors in a particular I am getting this error. This is running nod eone a ec2 instance

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added to [WebSocket]. Use emitter.setMaxListeners() to increase limit

This is my app, any time I put a ws call inside of a app.get or app.post I get this error, If I just put it outside those it works. Not sure why

const express = require("express");
const app = express();
const port = 8080;
const WebSocket = require("ws");
const url = "wss://g6q51519w2.execute-api.us-east-1.amazonaws.com/Prod";
const ws = new WebSocket(url);
var EventEmitter = require('events');


const ee = new EventEmitter()
ee.setMaxListeners(0);

ws.on("message", function incoming(data) {
    console.log(data);
  });


var hmm = {action:"sendmessage", data: "hello world"}

ws.on("open", function open(){
  ws.send(JSON.stringify(hmm))
})

app.get("/", (req, res) => {
  //var test = { action: "sendmessage", data: "hello world" };

  //ws.on("open", function open() {
    //ws.send(JSON.stringify(test));
  //});



  ws.on('error', (error) => {
    console.log(error)
  });

  res.send("Hello World!");
});

app.post("/doorbell", (req, res) => {
  var hmm1 = {action:"sendmessage", data: "hello world from post request"}

  ws.on("open", function open(){
    ws.send(JSON.stringify(hmm1))
  })
        res.send("heya hey");

});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lpincacommented, Nov 17, 2020

You can do something like this:

app.post('/doorbell', (req, res) => {
  var hmm1 = { action: 'sendmessage', data: 'hello world from post request' };

  if (ws.readyState === WebSocket.OPEN) {
    ws.send(JSON.stringify(hmm1));
    res.send('heya hey');
  } else {
    res.send('The WebSocket connection is not open');
  }
});
0reactions
wispycocommented, Nov 17, 2020

yeah sorry about that I orignally posted here and https://stackoverflow.com/questions/64868510/express-node-js-webssockets-is-recieving-messages-from-websocket-server-but-not but I really appreciate your help. I will close this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Websocket on("message") not working - Stack Overflow
I am trying to use it to talk client-server and then analyze that message serverside to broadcast a message server-clients. I can't figure...
Read more >
WebSocket.send() - Web APIs - MDN Web Docs - Mozilla
send () method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount ...
Read more >
Using WebSocket requests - Postman Learning Center
IO for a Socket.IO request. Enter the WebSocket server URL. A WebSocket URL begins with ws:// or wss:// . WebSocket URL. Select Connect....
Read more >
Part 1 - Send & receive - websockets 10.4 documentation
Part 1 - Send & receive#. In this tutorial, you're going to build a web-based Connect Four game. The web removes the constraint...
Read more >
WebSocket - Сучасний підручник з JavaScript
That's because ws:// data is not encrypted, visible for any intermediary. Old proxy servers do not know about WebSocket, they may see “strange”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found