Rainway Network

Rainway Network

The Rainway Network is how apps using Rainway find each other.

Each entity connected to the network is a Peer, identified by its Rainway Peer IDPeer ID - A 63-bit number that uniquely identifies it on the Rainway Network. The first time a Rainway SDK is initialized on a device, it requests a Peer ID and stores it on the device. Each time that device connects to the Rainway Network, the stored Peer ID will be used. If a stored Peer ID can no longer be found, a new Peer ID will be requested and stored..

Peers on the Rainway Network exchange connection requests and session information in order to establish a peer-to-peer connection. Once a connection between Peers has been established, they no longer use the Rainway Network to communicate.

First steps

Connecting to the Rainway Network happens automatically when you initialize RainwayRuntime.

RainwayRuntime is your way into the Rainway SDK. To initialize it, you must provide some configuration values and handlers through which Rainway and your app will communicate. This is done using the initialize static method on RainwayRuntime.

You can leave all the handlers empty for now. Just make sure you provide your apiKey.


By the way, externalId is a string identifying a user or entity within your app. This way you can easily tie an incoming connection back to a user in your app.

import { RainwayRuntime } from "rainway-sdk";

const runtime = await RainwayRuntime.initialize({
    apiKey: "your-organization's-api-key",
    externalId: "Jane Doe",

    onRuntimeConnectionLost: (runtime, error) => {},
    onConnectionRequest: (runtime, request) => {},
    onPeerMessage: (runtime, peer, channel, data) => {},
    onPeerDataChannel: (runtime, peer, channel, event) => {},
    onPeerError: (runtime, peer, error) => {},
    onPeerStateChange: (runtime, peer, state) => {},
    onStreamAnnouncement: (runtime, peer, announcement) => {},
    onStreamStop: (runtime, peer, stream) => {},

    logSink: (level, message) => console.log(`${level}: ${message}`),

At this stage, runtime is ready to use. It's time to get connected peer-to-peer!

Peer IDs

The first time RainwayRuntime is initialized on a device, it generates and stores a Peer ID. This is a 63-bit number that uniquely identifies the device on the Rainway Network.

To connect your device to another over Rainway, you must know the other's Peer ID. A device's Peer ID is exposed through the getPeerId method on RainwayRuntime.

const peerId = runtime.getPeerId();

How you store Peer IDs will vary depending on how your application uses Rainway. For example, you could store them in your application database to let Peers know about each other.

Connecting to a Peer

Once you have a Peer's ID, you can call connect to establish the connection:

const peer = await runtime.connect(remotePeerId);

This promise will resolve once the Peer has accepted your connection request.

To test against the example C# host app, try running it on another device and connecting from your web app to its peer ID.

Multiple Peers

Rainway lets you connect to multiple Peers at a time. The peers property on RainwayRuntime tracks every currently connected RainwayPeer, mapped by their Peer IDs.

Did this page help you?