0

I want to create a bidirectional communication between a frontend and a backend using websockets. I am using Node.js ws. Using

  wss.clients.forEach(function each(client) {
    client.send("This is a message")
  })

I am able to send a message to all clients with what I would call the "message" tag. Using Chrome Developer Tools, I can see that a ws packet with the ON_MESSAGE event is sent.

I would like to use a different kind of tag, however I can't find the documentation on how to do that, nor find anything on the internet that would explain a way of doing it. I tried things like the following, which did not work.

  wss.clients.forEach(function each(client) {
    client.emit("TAG", "This is a message")
  })
Skusku
  • 558
  • 5
  • 11

1 Answers1

1

You can achieve this by following a particular conversion for example

 const body = {
  tag: 'mytag',
  data: {
    message:"this is a message"
  }
};
 wss.clients.forEach(function each(client) {
client.emit(JSON.stringify(body))
})

so on upon receiving this data you can get the tag and the message on the body object

IamJMBassey
  • 61
  • 1
  • 5