0

hell0 there!

Today I tried to send a websocket message. What am I doing wrong? I commented the code bellow so hopefully you guys can understand my goal...

// Import websocket
const websocket = require('ws');

// Create server
var socket = new websocket.Server({ port: 8080 })

// When client connects to websock server, reply with a hello world message
socket.on('connection', ws => {
    ws.send('{"message":"Hello world."}'); //This works.
});

function send_message(msg){
    ws.send(msg);
}

/* Calculate something, await for user interaction, etc... */

// When im done with all that, just send a message.
send_message('{"message":"please work"}'); // This does not work

What am I doing wrong?

4
  • If you guys want I could list the countless ways I tried to make this work but I feel this would bloat up the question considerably! Commented May 27, 2020 at 3:12
  • Maybe show your actual code? The code above first sends a message via ws.send where ws is a parameter from a new connection. The send at bottom sends via some magic variable ws that you never show how it's set. If you want to send more messages you need to keep the first ws around somewhere like add it to a list of connections or pass it to some class that will manage it etc... Commented May 27, 2020 at 3:50
  • @gman ws isnt any magic variable? Its set at line 8. Of course, I know this variable can only be used within it's scope.. And thats why im making this question. how do you pass ws outside its scope? Commented May 27, 2020 at 3:59
  • It is a magic variable. You claimed it's what you tried but if you actually tried what you wrote it would have crashed with an error. Either ws undefined or null not a function Commented May 27, 2020 at 4:09

2 Answers 2

2

You need to keep track of the connections. How you do that is up to you

const websocket = require('ws');

const socket = new websocket.Server({ port: 8080 })

const connections = [];
socket.on('connection', ws => {
    connections.push(ws);
});

// send a new message to every connection once per second
setInterval(() => {
  const date = new Date();
  for (const ws of connections) {
    ws.send(`hello again: ${date}`);
  }
}, 1000);

Of course a real app would probably track the connections via something more complicated than just an array of connections. It would also need to stop tracking those connections when they disconnect etc...

Sign up to request clarification or add additional context in comments.

1 Comment

Been troubleshooting this for 2 days. Thanks for pointing me to the right track.
0
const socket = new WebSocket('server_url'); // Connection opened 
socket.addEventListener('message', function (event) { socket.send('send message'); });

I used the WebSocket like this to send the message.

I hope this will help you to fix the issues.

1 Comment

Thats client side code. This wont work on node.js. Besides, this thing still awaits an event to send a message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.