Slashtags Web Relay is a powerful abstraction library designed to simplify the management of data on Slashtags.
To install the package, use the following npm command:
npm install @synonymdev/web-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;
}
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());
})();
- Initialize a Client
- Access the Client's URL
- Generate a Slashtags URL
- Create or Update a File
- Delete a File
- Retrieve Data
- Monitor Updates to a File
- Terminate Subscriptions and Close the Client
const client = new Client({
keyPair: { secretKey: Uint8Array, publicKey: Uint8Array },
relay: "http://your-relay-address.com",
storage: "./path/to/storage",
});
console.log(client.url); // Outputs: slash:<client.id>/[?relay=<relay address>]
const myURL = await client.createURL("/examplePath");
console.log(myURL); // Outputs: Generated Slashtags URL
const options = {
encrypted: true,
awaitRelaySync: true,
};
await client.put("/examplePath", Buffer.from("Hello World"), options);
const deleteOptions = {
awaitRelaySync: true,
};
await client.del("/examplePath", deleteOptions);
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
const onupdate = (value) => {
console.log("Updated Value:", value);
};
const unsubscribe = coreData.subscribe(myURL, onupdate);
// To stop monitoring:
// unsubscribe();
await client.close();