rxjs-socket v1.0.2
RxJS Socket
With the help of this library, its possible to share RxJS Subscriptions between two connected applications, if both can exchange messages in form of strings.
These applications can be Websockets or just two Process on the same system.
Subscription Socket
The Subscription Socket class is the core of this library. By extending this class and the methods onMessage
and send
you can specify how the sockets communicate.
If the socket is closed the onClose
method should be called, which will close all open subscriptions and reset the resoures.
Observable Resource
To specify on what you can subscribe on both sockets the External
- and InternalResouces
are used.
For example a InternalResource
to observe an user could look like this:
{
user: ObservableResource<number, User>
}
So these resources are just objects of ObservableResource
s. To subscribe to this resource its now as simple as
subscriptionSocket
.observe('user', 1)
.subscribe(user =>
console.log(user)
)
The other socket now needs to provide this resource. This is done in the constructor:
constructor() {
super(
...,
{
user: id => theGetUserFunction(id)
}
)
}
Messages
Both sockets communicate by sending stringified message objects.
Those messages are split up in five types.
Here are some examples for the different types.
"subscribe"
subscribes to the selected resource if possible
{
"type": "subscribe",
"subscriptionId": 10,
"name": "user",
"data": 1
}
"unsubscribe"
unsubcribes from the selected subscription
{
"type": "unsubscribe",
"subscriptionId": 10
}
"next"
pushes the next value to the selected subscription
{
"type": "next",
"subscriptionId": 10,
"next": { "id": 1, "username": "Sample User" }
}
"error"
writes an error to the selected subscription
{
"type": "error",
"subscriptionId": 10,
"error": "unkown user with id 1"
}
"complete"
completes the selected subscription
{
"type": "complete",
"subscriptionId": 10
}
Examples
Browser Websocket
Only useable in the browser.Node Server Websocket
Represents a websocket connection on a websocket server. Depedens on the popular nodejs "ws" npm package.Node Websocket
Only useable in nodejs. Depedens on the popular nodejs "ws" npm package.
License
See the LICENSE file for license rights and limitations (MIT).