WebSockets on the IC
Seeing as the Internet Computer is the only blockchain to natively serve assets over http (as well as soon make outbound http calls to external services), we (PsychedelicDAO) think that the next natural continuation is to find a way to implement the WebSocket API. Not only would it provide tremendous value for services built on the IC, but it would add to the growing value prop that the IC has over other chains.
We want to be explicit that this post is not meant to act as a proposal for a technical implementation of the WebSocket API, but rather an attempt to lay out an initial vision for the community to run with.
Why WebSockets on the IC?
Currently, if a client wants to poll for data, it can create a loop that continuously requests data from an IC canister. This is exactly what we do in order to provide a somewhat live price feed for swaps on Sonic.
With websockets, clients wouldnât have to drive an unnecessary amount of requests to the IC. Instead, youâd be able to have services that can automatically, and on-chain if I might add, stream data between client and service.
Letâs take DSCVR, the popular social platform on the IC, and Plug, our wallet solution, as two examples to showcase the benefits of on-chain websockets.
DSCVR w/ websockets
DSCVR could become an extremely quick and snappy application with websockets - all likes, comments, and any other interaction could be loaded live. Additionally, on-chain audio streams could become a thing. Goodbye Twitter Spaces for all of web3.
Plug w/ websockets
Notifications. Plug could automatically plug in () to the dApps that youâve connected to and accept and display live push notifications via websockets.
High Level Implementation Details
Much like http calls, WebSockets on the IC would need to have protocols put in place for both inbound and outbound requests. Letâs dive into the difference between the two.
Inbound Websocket (Client Initiated)
Inbound websocket refers to a client that wants to initiate a websocket with a canister. A great example of this could be actual real time price fetching for tokens from Sonic.
In this scenario we would have the user initiate a request to open a websocket, at which time the boundary nodes would be responsible for calling ws_open
on the specified canister to open the websocket.
Once open, the boundary node can call ws_data
to write data from the client, and the canister can call ws_data
at the subnets management API to write to the client.
A close can be initiated by either the client to the boundary node, that would call ws_close
on the canister. The canister can call the management APIâs ws_close
to terminate the connection as well.
Outbound Websocket (Canister Initiated)
Outbound websocket refers to a canister opening up a websocket with an external websocket server (if we have inbound websockets at this time, could be another canister).
To open a new websocket, the canister calls ws_open
on the subnets management API, which passes the call along to the boundary node to initiate on external service.
Canister calls ws_data
on the management API to write to the websocket and the boundary node can call ws_data
on the canister to read from the websocket.
Close is initiated by the canister to the management APIâs ws_close
or by the boundary node to the canisterâs ws_close
method.
Unknowns
Subnet consensus is where things get tricky â we donât have a proposed solution here, but would love help from the community (we have heard OpenChat has worked on some WebRTC implementations) and the foundation to keep this conversation going forward.
We Need Your Help
We understand that this feature might be ambitious for the network and that there are currently a lot of other exciting and time consuming initiatives on the Internet Computerâs roadmap.
However, we have heard websockets on the IC brought up countless times in conversations with other builders in the community. We would like to start pushing the conversation forward now, so that when the time / resources are available, we have already ironed out some details.
Excited about websockets on the IC? Come join us for Psychedelic TownHall 03, August 11th (day of posting) at 1pm EST. Weâll be discussing this proposal and opening up the floor for thoughtful contributions from the community.