Streaming events

Request and immediately receive the events; terminate the stream when all relays satisfy the exit condition.

Initialize the stream

After building the client and connecting the relays, we can create a Filter and initialize the stream.

Rust
let filter: Filter = Filter::new().kind(Kind::Metadata).limit(10);
let mut stream = client.stream_events(filter, Duration::from_secs(10)).await?;
Python
# TODO: not supported yet
JavaScript
// TODO: not supported yet
Kotlin
// TODO: not supported yet
Swift
// TODO: not supported yet
C#
// TODO: not supported yet
Flutter
// TODO: not supported yet

Consume the stream

Now that we have the stream, we can consume it and handle the received Event:

Rust
while let Some(event) = stream.next().await {
    println!("{}", event.as_json());
}
Python
# TODO: not supported yet
JavaScript
// TODO: not supported yet
Kotlin
// TODO: not supported yet
Swift
// TODO: not supported yet
C#
// TODO: not supported yet
Flutter
// TODO: not supported yet

Full example

Rust
use std::time::Duration;

use nostr_sdk::prelude::*;

async fn stream() -> Result<()> {
    let client = Client::default();

    client.add_relay("wss://relay.damus.io").await?;
    client.connect().await;

    let filter: Filter = Filter::new().kind(Kind::Metadata).limit(10);
    let mut stream = client.stream_events(filter, Duration::from_secs(10)).await?;

    while let Some(event) = stream.next().await {
        println!("{}", event.as_json());
    }

    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    stream().await
}
Python
import asyncio
from datetime import timedelta

from nostr_sdk import Client


async def stream():
    client = Client()

    await client.add_relay("wss://relay.damus.io")
    await client.connect()

    # TODO: not supported yet

    # TODO: not supported yet


if __name__ == '__main__':
   asyncio.run(stream())
JavaScript
import {Client, loadWasmAsync} from "@rust-nostr/nostr-sdk";

async function stream() {
    await loadWasmAsync();

    let client = new Client();

    await client.addRelay("wss://relay.damus.io")
    await client.connect();

    // TODO: not supported yet

    // TODO: not supported yet
}

stream();
Kotlin
import kotlinx.coroutines.runBlocking
import rust.nostr.sdk.*

suspend fun stream() {
    val client = Client()

    client.addRelay("wss://relay.damus.io")
    client.connect()

    // TODO: not supported yet

    // TODO: not supported yet
}

fun main() {
    runBlocking { stream() }
}
Swift
import Foundation
import NostrSDK

func stream() async throws {
    let client = Client()

    try await client.addRelay(url: "wss://relay.damus.io")
    await client.connect()

    // TODO: not supported yet

    // TODO: not supported yet
}
C#
using Nostr.Sdk;

public class Stream
{
    public static async Task Run()
    {
        var client = new Client();

        await client.AddRelay("wss://relay.damus.io");
        await client.Connect();

        // TODO: not supported yet

        // TODO: not supported yet
    }
}
Flutter
import 'package:nostr_sdk/nostr_sdk.dart';

Future<void> stream() async {
  Client client = Client();

  await client.addRelay(url: "wss://relay.damus.io");
  await client.connect();

  // TODO: not supported yet

  // TODO: not supported yet
}