Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/voxoco/durable-stream-client
Client NodeJS application for durable-stream
https://github.com/voxoco/durable-stream-client
cloudflare-r2 cloudflare-workers durable-objects persistence websocket
Last synced: 2 months ago
JSON representation
Client NodeJS application for durable-stream
- Host: GitHub
- URL: https://github.com/voxoco/durable-stream-client
- Owner: voxoco
- License: mit
- Created: 2022-09-03T19:09:10.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-06T02:02:01.000Z (over 2 years ago)
- Last Synced: 2024-11-13T14:55:39.317Z (3 months ago)
- Topics: cloudflare-r2, cloudflare-workers, durable-objects, persistence, websocket
- Language: JavaScript
- Homepage: https://voxo.co
- Size: 14.6 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Durable Stream Client :electric_plug:
A lightweight client for [Durable Stream](https://github.com/voxoco/durable-stream)
The client
## Installation
```bash
npm install durable-stream-client
```## Feaures
* Publish messages to a durable stream (to be propagated to other clients subscribed on the same subject)
* Simple reconnect logic
* Queues up messages while disconnected and sends them when reconnected (with a ~2 second timeout)
* Subscribe to messages on a durable stream
* Delete messages from a durable stream
* Get/set the object state (a generic object we can set/get on the durable object)
* Get metadata, get, put, delete objects in R2## Usage
```js
// Shim the websocket client for node
globalThis.WebSocket = require('websocket').w3cwebsocket;import DurableStreamClient from 'durable-stream-client'
const client = new DurableStreamClient({
host: '.voxo.workers.dev', // defaults to localhost:8787
secure: true, // boolean required (if local set to false)
apiKey: 'my-api-key', // string required
subject: 'my-subject', // string required
})// Initialize the client
await client.init();
```## Primary Stream Methods
```js
// Get the current sequence number and general stream info
const info = await client.info();
console.log(info);const msg = {
test: 'value',
someData: 'some-data',
}// Publish a message (can be a string or object)
const res = await client.publish(msg)
// Returns a promise that resolves to the response from the server and includes the message id, sequence number etc..// Subscribe to messages
// The first arg is the sequence number to start from (0 for all messages from the beginning of the stream)
client.subscribe(10000000019, async (msg, ack) => {
console.log(`Received message: ${JSON.stringify(msg)}`);
ack();
// Be sure to ack all messages!
// Acknowledging a message will remove it from the queue on the client and server
})// Unsubscribe from messages
await client.unsubscribe();// Delete messages in the stream up to a sequence number
await client.delete(10000000019);// Get the object object state (just a generic object we can set/get on the durable object)
const state = await client.getState();// Set the object state
await client.putState({ some: 'data' });
```## R2 Methods
```js
// Head object (get metadata)
const metadata = await client.headObject('/path/to/object.ext');// Get object
const object = await client.getObject('/path/to/object.ext');
// Write the file to disk
fs.writeFileSync('/local/path/file.ext', object);// Put object
// Arg 1 = file path in R2
// Arg 2 = local file path to uploadconst res = await client.putObject('/path/to/object.ext', '/local/path/file.ext');
// Delete object
const res = await client.deleteObject('/path/to/object.ext');
```##
## License
MIT