Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data to different websocket endpoints in one connection

i have a question about websockets and sending data to different backend-endpoints in Javascript.

So is it possible to send data to different backend-endpoints in one websocket connection or do i need to establish a new connection for every endpoint?

Lets say:

var ws = new Websocket('ws://localhost:8080');

// send data to /login
ws.send('/login', {logindata...});

// send data to /messages
ws.send('/messages', {data...});
like image 286
Der_D Avatar asked Oct 20 '25 11:10

Der_D


1 Answers

I think what you must do is define a message protocol so the server execute the correct endpoint for each type of message.

For example:

{
   "msgType": "login",
   "msgData": {logindata...}
}

and

{
   "msgType": "messages",
   "msgData": {data...}
}

Those could be you two type of messages you send to the server. And in the backend, have a controller to execute de "login/" endpoint or the "messages/" endpoint.

I don't know if that helps... That's the way I do it.

like image 92
Ion Gorostizu Avatar answered Oct 22 '25 23:10

Ion Gorostizu