Skip to main content

Rust

pubsub-rust is a Rust library for the Synternet Data Layer project that enables 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 Rust applications with the Synternet Data Layer platform.

Installation

To use the Rust SDK for Data Layer in your project, add the following dependency to your Cargo.toml file:

[dependencies]
synternet_pubsub = "0.1.0"

Getting Started

Before you begin using the Rust SDK, make sure you have the necessary credentials and access tokens from the Synropy Developer Portal platform. These credentials will allow you to connect to the Data Layer and subscribe to or publish data streams.

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.

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

cargo.toml

[package]
name = "testrust"
version = "0.1.0"
edition = "2021"

[dependencies]
async-nats = "0.29.0"
tokio = { version = "1.25.0", features = ["full"] }
pubsub-rust = { git = "https://github.com/synternet/pubsub-rust.git", branch = "main" }
futures = { version = "0.3.26", default-features = false, features = ["std", "async-await"] }
bytes = "1.4.0"

Publish

extern crate pubsub_rust;

use bytes::Bytes;
use std::time::Instant;
use tokio::time::{sleep, Duration};
use std::io::{self, Write};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let access_token = "SAAGNJOZTRPYYXG2NJX3ZNGXYUSDYX2BWO447W3SHG6XQ7U66RWHQ3JUXM";
let nats_server_ip = "nats://127.0.0.1";
let subject = String::from("example.subject");

let client = pubsub_rust::connect(nats_server_ip, access_token).await?;

let now = Instant::now();
let data = Bytes::from("bar");
for _ in 0..100 {
print!("publishing\n");
io::stdout().flush().unwrap();
client.publish(subject.clone(), data.clone()).await?;
sleep(Duration::from_secs(1)).await; // Wait for 1 second before the next publish
}
client.flush().await?;

println!("published in {:?}", now.elapsed());

Ok(())
}

Subscribe

extern crate pubsub_rust;
use std::time::Instant;
use tokio;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

let access_token = "SAAGNJOZTRPYYXG2NJX3ZNGXYUSDYX2BWO447W3SHG6XQ7U66RWHQ3JUXM";
let nats_server_ip = "nats://127.0.0.1";
let subscribe_subject = "example.subject";

let client = pubsub_rust::connect(nats_server_ip, access_token).await?;

let now = Instant::now();
let mut subscriber = client.subscribe(subscribe_subject.into()).await.unwrap();

println!("Awaiting messages");
// Import StreamExt here:
use futures::stream::StreamExt;
while let Some(message) = subscriber.next().await {
println!("Received message {:?}", message);
}

println!("subscriber received in {:?}", now.elapsed());

Ok(())
}

Those examples demonstrate how to connect to a NATS server, subscribe to a subject, and publish messages to the subject using the Synternet PubSub-Rust library.

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 Rust SDK for Data Layer, please reach out to our support team at Discord #developers-chat. 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 Rust SDK for the Data Layer. Happy coding with real-time data streams and enjoy the power of the Data Layer in your Rust applications!