Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support subprotocols without synchronized sequence ID #39

Open
PMalte opened this issue Sep 13, 2021 · 0 comments
Open

Support subprotocols without synchronized sequence ID #39

PMalte opened this issue Sep 13, 2021 · 0 comments

Comments

@PMalte
Copy link

PMalte commented Sep 13, 2021

I want to use this library with a subprotocol where the client and server have their own sequence ID. Both peers should be able to send requests and receive corresponding responses.

The problem I encounter:
The subprotocol is based on request-response pattern.
Imagine both peers use their own sequenceId to number their requests and both peers start with sequenceId = 1.
So it is possible, that both peers send a request with the same sequence ID at the same time. Now the client considers the request of the server as the response of its request and the real response of the server is dismissed.

This happens because the assignment of messages is only based on the sequence ID.

Feature Request:
Give the caller the opportunity to save additional data to every request so the assignment of messages is not only based on the sequence ID but on additional data.

Example:

Request opcode Possible response opcodes
login ack, nak
getProperties properties, nak
... ...

If I send a login command and it gets the sequenceId = 34, I can save the possible response opcodes as additional data for that request and when I get a response matched by the sequenceId I can validate the response by checking if the response opcode matches ack or nak.

const wsp = new WebSocketAsPromised(wsUrl, {
  packMessage: data => JSON.stringify(data),
  unpackMessage: data => JSON.parse(data),
  attachRequestId: (data, requestId) => Object.assign({id: requestId}, data), // attach requestId to message as `id` field
  extractRequestId: data => data && data.id,                                  // read requestId from message `id` field
  isPossibleResponse: (data, additionalData) => additionalData.possibleResponses.includes(data.opcode) // <--- new parameter
});

wsp.sendRequest({foo: 'bar'},{requestId: 42, additionalData: {possibleResponses: ['ack', 'nak']}});

I worked a bit on this idea in the following fork: https://github.com/PMalte/websocket-as-promised/tree/feature/bidirectional-subprotocols

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant