1.0.9 • Published 5 months ago

@synonymdev/web-relay v1.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Slashtags Web Relay

Slashtags Web Relay is a powerful abstraction library designed to simplify the management of data on Slashtags.

📦 Installation

To install the package, use the following npm command:

npm install @synonymdev/web-relay

🚀 Getting Started

1. Running the Relay

Copy and customize the configuration file.

cp config/config.example.json config/config.json

Run npm start or use pm2 pm2 start ecosystem.config.json

Setup proxy:

Web-relay uses Server-sent Events to subscribe for updates, which needs HTTP/2 to work best (especially in browsers).

If you are using Nginx, consider using the following configuration:

  location <path here> {
    proxy_pass <relay host:port here>;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    chunked_transfer_encoding off;
    proxy_buffering off;
    proxy_cache off;
  }

2. Interacting via the Client

const { Client } = require("@synonymdev/web-relay/client");

const alice = new Client({ relay: "http://localhost:3000" });

(async () => {
  const url = await alice.createURL("/foo");
  console.log("Generated URL:", url);

  await alice.put("/foo", Buffer.from("bar"));
  const saved = await alice.get("/foo");
  console.log("Saved Data:", saved.toString());

  const bob = new Client();
  const resolved = await bob.get(url);
  console.log("Resolved Data:", resolved.toString());
})();

📚 API Documentation

Table of Contents


Initialize a Client

const client = new Client({
  keyPair: { secretKey: Uint8Array, publicKey: Uint8Array },
  relay: "http://your-relay-address.com",
  storage: "./path/to/storage",
});

Access the Client's URL

console.log(client.url); // Outputs: slash:<client.id>/[?relay=<relay address>]

Generate a Slashtags URL

const myURL = await client.createURL("/examplePath");
console.log(myURL); // Outputs: Generated Slashtags URL

Create or Update a File

const options = {
  encrypted: true,
  awaitRelaySync: true,
};
await client.put("/examplePath", Buffer.from("Hello World"), options);

Delete a File

const deleteOptions = {
  awaitRelaySync: true,
};
await client.del("/examplePath", deleteOptions);

Retrieve Data

const getOptions = {
  skipCache: false, // Set to `true` to Skip the local cache and wait for the remote relay to respond with fresh data.
};
const data = await client.get(myURL, getOptions);
console.log(data); // Outputs: Retrieved data

Monitor Updates to a File

const onupdate = (value) => {
  console.log("Updated Value:", value);
};
const unsubscribe = coreData.subscribe(myURL, onupdate);
// To stop monitoring:
// unsubscribe();

Terminate Subscriptions and Close the Client

await client.close();
1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago

0.2.0

9 months ago

0.1.0

10 months ago