Reputation: 121
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
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