Skip to main content

JavaScript

caution

This section of the documentation is outdated and needs to be updated.

synternet-pubsub-js is a TypeScript library for the Synternet Data Layer project that allows you to subscribe to existing data streams or publish new ones. This library is built on top of the NATS messaging system and provides a convenient way to integrate your TypeScript applications with the Synternet Data Layer platform.

Installation

Clone this example repository to get started with Node.js SDK

git clone https://github.com/synternet/pubsub-js.git

Getting Started

Before you begin using the JavaScript SDK, make sure you have the necessary credentials and access tokens from the Synternet developer portal. These credentials will allow you to connect to the Data Layer and subscribe to or publish data streams.

Usage

The preferred method of authentication is using an access token from the developer portal.

import { NatsService } from "../pubsub/nats";
import { createAppJwt } from "../pubsub/userJwt";

const natsUrl = "url-to-nats.com";
const subject = "hackathon.mysubject";
const accessToken = `SAAGYGEENOBBBBSPZDVVVYEUV3R4LAAAIEYVJOYXMWYJD6YQ5N3LVMQSA4`;

async function printData(data: Uint8Array): Promise<void> {
const decoded = new TextDecoder().decode(data);
console.log(`Received message on ${subject} subject. Message: ${decoded}`);
}

async function main() {
// Connect to the NATS server with credentials
const service = new NatsService({
url: natsUrl,
natsCredsFile: createAppJwt(accessToken),
});

console.log("Connecting to NATS server...");
await service.waitForConnection();
console.log("Connected to NATS server.");

// Add a handler function to process messages received on the exampleSubscribeSubject
console.log(`Listening for ${subject} messages...`)
service.addHandler(subject, async (data: Uint8Array) => {
await printData(data);
});

// Start serving messages and processing them using the registered handler function
await service.serve();
}

main().catch((err) => {
console.error("Error:", err);
process.exit(1);
});

Examples

For detailed usage examples, please refer to the examples directory in the repository. These examples cover various scenarios and demonstrate how to utilize the SDK's features effectively.

Here is a simple example demonstrating how to subscribe to a data stream using seed from developer portal (the preferred method of authentication is using an access token from the developer portal):

import { NatsService } from "../pubsub/nats";
import { createAppJwt } from "../pubsub/userJwt";

const natsUrl = "url-to-nats.com";
const subject = "hackathon.mysubject";
const accessToken = `SAAGYGEENOBBBBSPZDVVVYEUV3R4LAAAIEYVJOYXMWYJD6YQ5N3LVMQSA4`;

async function printData(data: Uint8Array): Promise<void> {
const decoded = new TextDecoder().decode(data);
console.log(`Received message on ${subject} subject. Message: ${decoded}`);
}

async function main() {
// Connect to the NATS server with credentials
const service = new NatsService({
url: natsUrl,
natsCredsFile: createAppJwt(accessToken),
});

console.log("Connecting to NATS server...");
await service.waitForConnection();
console.log("Connected to NATS server.");

// Add a handler function to process messages received on the exampleSubscribeSubject
console.log(`Listening for ${subject} messages...`)
service.addHandler(subject, async (data: Uint8Array) => {
await printData(data);
});

// Start serving messages and processing them using the registered handler function
await service.serve();
}

main().catch((err) => {
console.error("Error:", err);
process.exit(1);
});

Contributing

We welcome contributions from the community! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository. We appreciate your feedback and collaboration in making this SDK even better.

Support

If you encounter any difficulties or have questions regarding the Python SDK for Data Layer, please reach out to our support team at Discord #developer-discussion. We are here to assist you and ensure a smooth experience with our SDK.

We hope this documentation provides you with a comprehensive understanding of the JavaScript SDK for the Data Layer. Happy coding with real-time data streams and enjoy the power of the Data Layer in your JavaScript applications!