-
Notifications
You must be signed in to change notification settings - Fork 43
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
WebSocket Subscriptions via JRPC #107
WebSocket Subscriptions via JRPC #107
Conversation
9189c75
to
72b8bcb
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #107 +/- ##
==========================================
+ Coverage 90.46% 94.34% +3.88%
==========================================
Files 20 25 +5
Lines 1752 2212 +460
Branches 208 291 +83
==========================================
+ Hits 1585 2087 +502
+ Misses 167 125 -42 ☔ View full report in Codecov by Sentry. |
9c8f504
to
55674ae
Compare
763410d
to
f54fac2
Compare
@thehenrytsai The biggest change I've made was removing the I chose to model this change in an attempt to be compliant with the JsonRpc spec guidelines for extensions. Looking to see how they normally extend the spec's capabilities I came across this: So we've added an optional To close this long-running subscription we initiate an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an impactful one!!!
🐐🐐🐐 🎖️ 🎖️ 🎖️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐐 🐐 🐐
This PR enhances
WebSocket
support viaJSON RPC 2.0
.This now supports long running subscriptions via
JSON RPC
.The new
JSONRPCSocket
abstracts this by providing two methods,request
andsubscribe
.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, 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 hothttp
.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.getDWNConfig
,tenantGate
andeventStream
were both made optional, as well as theregistrationManager
forHttpApi
. 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.subscriptionRequest
method withintests/utils.ts
will be replaced by a full-fledge client, listed in a separate PR below. Less attention was paid to making this usable anywhere outside of the current tests.sendHttpMessage
method withintests/utils.ts
will also be replaced by full-fledged client, listed in a separate PR below.Usage of
subscription.close
JSON RPC method.Q: Why not use a specific
Unsubscribe
DWN message such asRecordsUnsubscribe
?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 specificJSON RPC Id
. This JSON RPC Id represents a specific request to a transport that was created. Later a user can issue asubscription.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:
Message
to authorize aSocketConnection
open. #111HttpDwnRpcClient
from@web5/agent
to thedwn-server
repo. #109WebSocketDwnRpcClient
to handle WebSocket connections. #110Separate effort:
RecordsWrite
support for WebSocket via JRPC. #108