0

I am trying to update a REST API to API with Websockets and I don't understand how to handle the responses.

With REST API and awaits is easily identify request with response.

But in WebSocket it seems that all the communication is treated in onmessage, mixed and I don't see how to identify it.

How to identify a concrete answer without id in the request?

For example, I connect to a WebSockets API, in this case Kraken (exchange) and once the connection is open, I add an order:

ws.send(JSON.stringify({
                'event' : 'addOrder',
                'ordertype' : 'limit',
                'pair' : 'DAI/USD',
                'price' : '0.005',
                'token' : this.wsToken,
                'type' : 'buy',
                'volume' : '6',
            }));

And the response obtained in onmessage can be:

{"descr":"buy 6.00000000 DAIUSD @ limit 0.00500","event":"addOrderStatus","status":"ok","txid":"ABABC-ABABC-ABABC"}

The request has no Id, if I launch 10 requests for addOrder and other types I cannot link the request with the response.

What is the logic to work with an API in this way?

ephramd
  • 561
  • 2
  • 15
  • 41
  • The webSocket protocol is not a request/response protocol like http is - it's a messaging protocol. If you want to simulate a request/response protocol, then you will have to insert your own unique ID in every request message and the server will have to return that same ID in the response. Then, your receiving code will have to build a layer to connect the two and you can wrap that in a promise if you want to use `await` for the response. – jfriend00 Jun 11 '21 at 18:01
  • 2
    FYI, `socket.io` (which is a layer on top of the webSocket protocol) does this already for you. You can send a message in socket.io and get a callback when you get the response to that specific message. A simple function wrapper could turn that callback into a promise. – jfriend00 Jun 11 '21 at 18:14
  • @jfriend00 Being a third-party API I see that the option to send an id or any other parameter that they do not mention is rejected. Socket.io's solution sounds interesting, thanks! I will check it – ephramd Jun 11 '21 at 20:07
  • If you're using a non- customizable api and it does not already have the desired unique ID in both request and response, then it's not a request response api and there's not much you can do about that. – jfriend00 Jun 11 '21 at 20:26
  • Also, you cannot connect a socket.io client to a websocket server. It will only connect to a socket.io server – jfriend00 Jun 11 '21 at 20:28

1 Answers1

0
useEffect(() => {
    new WebSocket('wss://ws.kraken.com').onopen = function () {
      this.onclose = () => console.log('SOCKET CLOSED');
      this.onmessage = (e) => console.log(JSON.parse(e.data));
      this.send(
        JSON.stringify({
          event: 'subscribe',
          pair: ['XBT/USD', 'XBT/EUR', 'ADA/USD'],
          subscription: { name: 'ticker' },
        }),
        (e) => console.log(e),
      );
    };
  }, [isPaused]);
Rishabh Deep Singh
  • 807
  • 1
  • 12
  • 24
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 06 '22 at 14:47