ReSynkd
Observable pattern through WebSockets.
Motivation
ReSynkd puts together the amazing RxJS library and the WebSocket protocol.
Although RxJS provides the WebSocketSubject class, it seems that anyone can subscribe to but also send through the same webSocketSubject, which I did not like. I wanted a way to put an Observable on the server and subscribe to it from the clients, but also vice versa.
Documentation
The typedoc documentation is available at https://cope.github.io/resynkd/docs/
Demo
See the ./demo/
folder in this repo.
Usage
Keep in mind that the socketsSendMethod
is a placeholder for whatever the send
method is in the websocket implementation used,
and thus this function varies.
Most often it is simply socket.send
, as is in the demo, but it can also be connection.socket.send
or e.target.send
, and so on.
Endpoint1 (Observable)
;const resynkd = ;socket; const subject = ;resynkd;subjectnext'value 1';subjectnext'value 25';subjectnext'value 17';// etc.
Endpoint2 (Observer)
;const resynkd = ;socket; resynkd;
The on message processing
The main part of any websocket communication is the on message
event.
Whether it is socket.on('message', ...
, or socket.addEventListener('message', ...
,
in either case, the handler method receives the websocket message and it must include the resynkd processing in it.
;const resynkd = ;let { let consumed = resynkd; // IMPORTANT: don't forget to bind! if!consumed console; // ...handle non-resynkd messages...}
Putting it all together, the most common example looks like below...
Server side (using fastify)
;const resynkd = ;fastify;
Client side
const ReSynkd = ;const resynkd = ; let socket = YOUR_WS_URI;socket { let data: message = e; let consumed = resynkd; // IMPORTANT: don't forget to bind! if!consumed console; // ...handle non-resynkd messages...};
TODO
See the Kanban board
- Peer-to-Peer (client-to-client) subscriptions
- Observables broadcast their subjects
- so that observers know what they can subscribe to