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

Create a WebSocketDwnRpcClient to handle WebSocket connections. #110

Open
LiranCohen opened this issue Feb 13, 2024 · 0 comments
Open

Create a WebSocketDwnRpcClient to handle WebSocket connections. #110

LiranCohen opened this issue Feb 13, 2024 · 0 comments
Assignees
Labels
WIP work in progress

Comments

@LiranCohen
Copy link
Member

LiranCohen commented Feb 13, 2024

Similarly to Bring HttpDwnRpcClient from @web5/agent to the dwn-server repo create a WebSocketDwnRpcClient that will handle socket connections.

@LiranCohen LiranCohen added the WIP work in progress label Feb 13, 2024
@LiranCohen LiranCohen self-assigned this Feb 13, 2024
LiranCohen added a commit that referenced this issue Feb 27, 2024
This PR enhances `WebSocket` support via `JSON RPC 2.0`.

This now supports long running subscriptions via `JSON RPC`.
The new `JsonRpcSocket` abstracts this by providing two
methods,`request` and `subscribe`.

`request` sends a JSON RPC message over the socket, setting up a message
handler to listen to the response, once the response is received it will
clean up the listener, and resolve the response to the promise. If a
response is not emitted within a given timeout, it will reject the
promise.

`subscribe` does something similar, however after receiving the response
to the subscribe message, it will keep the handler open and look for any
other messages that match the JSON RPC Id provided in the subscription request, and emit those to a
message handler. The subscribe method returns a close method in order to
clean up these listeners.

Some things to note:
- `RecordsWrite` are currently not supported via sockets, this is due to
a poor handling of the data payload in the prior implementation. Would
rather add this as a separate effort. TODO is tagged in code with the
issue listed below.
- `Subscribe` methods are currently only supported via sockets and hot
`http`.
- A `rpc.subscription.close` JSON RPC Method was added to close a
subscription that is active for a connection. I went back and forth
between making this a DWN Message vs some other signal. I landed on this
for the time being and am open to discussion. More notes below.
- As a part of `getDWNConfig`, `tenantGate` and `eventStream` were both
made optional, as well as the `registrationManager` for `HttpApi`. I did
this mostly out of convenience, but it also seems plausible that someone
might run this without any registration manager. Open to discuss/change.
- the `sendHttpMessage` method within `tests/utils.ts` will also be
replaced by full-fledged client, listed in a separate PR below.
- Current implementation allows anyone to connect, this will be
addressed in a subsequent PR, issue listed below.

### Usage of `subscription.close` JSON RPC method.

Q: Why not use a specific `Unsubscribe` DWN message such as
`RecordsUnsubscribe`?
A: This would be the first message of it's kind that would need to
specifically target the DWN and potentially transport of the DWN which
holds the subscription. Instead the DWN `RecordsSubscribe` message
returns a close method which the transport can keep a reference to given
a specific `JSON RPC Id`. This JSON RPC Id represents a specific request
to a transport that was created. Later a user can issue a
`rpc.subscription.close` JSON RPC Method to tell the server to close that
subscription.

Ultimately the owner of the JRPC Socket connection is in charge of
closing the subscription, they can close all subscriptions by simply
disconnecting. So it makes sense to give them the ability to close a
specific JRPC Subscription.

## Initial Effort Subsequent Issues/PRs:
- #111
- #109
- #110

### Separate effort:
-  #108
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WIP work in progress
Projects
None yet
Development

No branches or pull requests

1 participant