Der_D
Der_D

Reputation: 121

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...});

Upvotes: 3

Views: 1546

Answers (1)

Ion Gorostizu
Ion Gorostizu

Reputation: 116

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.

Upvotes: 2

Related Questions