Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust SDK

Crate: fastpubsub-sdk

[dependencies]
fastpubsub-sdk = "0.3"

The crate name on crates.io is fastpubsub-sdk. In Rust the import is fastpubsub_sdk.

Default features: REST discovery, WebSocket, access-token JSON.

fastpubsub-sdk = { version = "0.3", features = ["webtransport"] }

Session, token, socket

#![allow(unused)]
fn main() {
use fastpubsub_sdk::client::{open, FastPubSub};
use fastpubsub_sdk::transport::{SubscribeOptions, WebSocketTransport};
use fastpubsub_sdk::{CreateAccessTokenBuilder, TenantGrant};

let mut session = open("globaltest");
session.resolve_edge().await?;

let access_token = CreateAccessTokenBuilder::new()
    .created_by("my-rust-app")
    .expires_at_input("+1h")?
    .tenant_grant(
        TenantGrant::new(vec!["app".to_string()])
            .allow_pub(vec!["public.#".to_string()])
            .allow_sub(vec!["public.#".to_string()]),
    )
    .create_with_config(session.api_base()?, &master_token, session.http_config())
    .await?;

let mut client: FastPubSub<WebSocketTransport> =
    session.web_socket(access_token)?.build().await?;

let mut rx = client
    .subscribe("app", "public.#", &SubscribeOptions::default())
    .await?;

client.publish("app", "public.chat", b"hello").await?;
}

Call resolve_edge() before web_socket. The Rust builder does not pick an edge by itself.

Publish modes

#![allow(unused)]
fn main() {
use fastpubsub_sdk::transport::{PublishDeliveryMode, PublishOptions};

client
    .publish_with_options(
        "app",
        "public.chat",
        b"hello",
        &PublishOptions {
            delivery: PublishDeliveryMode::DeliverOneLowLatency,
            ..Default::default()
        },
    )
    .await?;
}
ModeBehaviour
BroadcastAll matching subscribers (v1 frame)
DeliverOneLowLatencyOne overlay node with lowest inter-node latency among nodes that have subscribers
DeliverOneRandomOne random overlay node with subscribers

HTTP GET /ping is for edge selection. After connect:

#![allow(unused)]
fn main() {
let client = session
    .web_socket(access_token)?
    .ping_interval_secs(3)
    .build()
    .await?;
}

Allowed intervals: 1, 3, or 5 seconds. Omit the call to disable application ping.

REST helpers

MethodPathAuth
GET/pingnone
POST/v1/get-tokenmaster token
PUT/v1/refresh-tokenmaster token
DELETE/v1/revoke-tokenmaster token

refresh_access_token / refresh_access_token_from_at and revoke_access_token live next to the create-token helpers. Parse AT_{id}_{secret} with parse_access_token / access_token_id.

Filters and WebTransport