How to initialize a single WebSocket connection for multiple requests to the server?

There is a class that organizes the requests to the server. Among the methods there is one that is responsible for WebSocket. This method takes parameters and initialisere the connection with the server.

Requests.prototype.wsQuery = function(msg, callback) {
 // Initialized each time you call the method. Nonsense!
 var w = new ws('wss://api.something.com/ws/');

 w.on('open', function() {
w.send(JSON.stringify(msg));
});

 w.on('message', function(res) {
callback(res);
 }); 
}

// ***

module.exports = new Requests;


The problem is that when multiple occurrences, the connection crashes `error 429`, i.e. too many requests to the server...

How to organize the code that would initialize a WebSocket happened just once? If each ad of the Request object to specify parameters for a WebSocket? Or maybe something else is possible?

const w = new ws('wss://api.something.com/ws/');

let obj = Request(w);
let obj_2 = Request(w);
// ***
June 14th 19 at 20:47
1 answer
June 14th 19 at 20:49
Do not quite understand why to create multiple objects Request and why not do all requests in one.

But if indeed it should be - your version is not so bad.

You can hide the initialization of an object in a function:
function getRequestFactory() {
 const ws = initWSConnection();
 return () => { return new Request(ws) }
}


You can still come up with something....
In one object all queries? I then need to sort the responses from the server. The answers are of this type `[channel , [data]]`. If one object is to throw all the queries, then the answer will receive a lot of channels and is not clear to which request belongs to the specified channel.

If for each request to create an object, it is possible to somehow sort the answers.

And so, the function is interesting, but I would like not to paste in each WebSocket object. Maybe you can do something from within the class to build a WebSocket initialized, and then distribute it to all objects of this class Request? - theo8 commented on June 14th 19 at 20:52
Don't know what application might this approach justifies itself. But to keep multiple connections from one client can be not a good idea.
Perhaps in the handler of messages from the server, you can implement some kind of routing (via the parameter channel as I understand it), what class - the originator (subscriber) request to return a response.
I would recommend think how it can be implemented within a single compound and to test how many simultaneously connections from one client can keep.

Something like that (consider it as pseudocode):
class WS {
 constructor() {
 this.ws = initWS();
 this.subscribers = {};
}
 subscibe(channel, subscriber) {
 this.subscribers[channel].push(subscriber) //of course, necessary to provide the initialization of the array to the first subscriber
}

 messageHandler(messagePayload) {
 this.subscribers[messagePayload.channel].forEach(subscriber => subscriber.notify(messagePayload))
}
}

subscriber.notify - handler messages for a specific channel.


And so, the function is interesting, but I would like not to paste in each WebSocket object. Maybe you can do something from within the class to build a WebSocket initialized, and then distribute it to all objects of this class Request?


you can initialize the connection in the global scope and Request to use it, but it differs from the version with the function in the worst (IMHO) way. - marcus.Wel commented on June 14th 19 at 20:55

Find more questions by tags JavaScriptWebSocket