Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rxtoolkit/ws
🔌 RxJS operators for working with WebSockets
https://github.com/rxtoolkit/ws
fp functional-programming observables package reactive-programming rxjs websocket websockets
Last synced: 7 days ago
JSON representation
🔌 RxJS operators for working with WebSockets
- Host: GitHub
- URL: https://github.com/rxtoolkit/ws
- Owner: rxtoolkit
- License: mit
- Created: 2021-05-10T01:50:42.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-03-27T12:38:50.000Z (8 months ago)
- Last Synced: 2024-09-23T01:53:16.951Z (about 2 months ago)
- Topics: fp, functional-programming, observables, package, reactive-programming, rxjs, websocket, websockets
- Language: JavaScript
- Homepage:
- Size: 583 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Contributing: docs/CONTRIBUTING.md
Awesome Lists containing this project
README
# @rxtk/ws
> 🔌 RxJS operators for working with WebSockets```bash
npm i @rxtk/ws
``````bash
yarn add @rxtk/ws
```## API
### `conduit`
Opening a two-way WebSocket is a very common software pattern. The `conduit` operator makes it trivially easy to do this. It pipes an Observable into a two-way websocket. Each item in the input Observable will be published to the server. The output Observable will be the messages sent from the server to the client.Optionally, it can be performed using a custom serializer or deserializer -- otherwise, it will assume the messages are encoded/decoded as JSON and transmitted as JSON strings.
The conduit also supports error handling and many disconnection scenarios (see examples below).
#### Basic usage
```javascript
import { from } from 'rxjs';
import { conduit } from '@rxtk/ws';const messagesToSend$ = from([
{body: 'data'},
{body: 'more data'},
]);
const socketResponse$ = messageToSend$.pipe(
conduit({url: 'wss://mysite.com'})
);socketResponse$.subscribe(); // this will attempt to send the messages to the server
```#### With error handling
```javascript
import { from } from 'rxjs';
import { conduit } from '@rxtk/ws';const messagesToSend$ = from([
{body: 'data'},
{body: 'more data'},
]);
const socketResponse$ = messageToSend$.pipe(
conduit({url: 'wss://mysite.com'})
);socketResponse$.subscribe(); // this will attempt to send the messages to the server
socketResponse$.error$.subscribe(); // returns WebSocket errors
```#### Custom serialization
```javascript
import { from } from 'rxjs';
import { conduit } from '@rxtk/ws';const decodeMessage = base64Message => atob(base64Message);
const encodeMessage = binaryString => btoa(binaryString);const messagesToSend$ = from([
'somebinarystring',
'anotherbinarystring',
]);
const socketResponse$ = messageToSend$.pipe(
conduit({
url: 'wss://mysite.com',
serializer: encodeMessage,
deserializer: decodeMessage,
})
);
socketResponse$.subscribe();
```