Exchanging Messages

Main article: Data Channels

Exchanging Messages and Data

Rainway provides Peers with the ability to create Data Channels, over which they can exchange arbitrary application-level binary data.

Sending messages over this channel is permitted any time two peers are connected end-to-end, whether they're streaming or not.


The peer that requested the peer-to-peer connection is responsible for creating Data Channels.

peer.createDataChannel("Messages", RainwayChannelMode::Reliable);

The peer that accepted the connection request then gets an onPeerDataChannel event for each created channel.

config.onPeerDataChannel = [this](const auto peer, const auto channel, auto mode) {
    auto name = peer.externalId();
    std::cout << "New data channel '" << channel << "' created by remote peer " << name << std::endl;

To send a message to a peer, use the send method on RainwayPeer:

const char message[3] = { 1, 2, 3 };
peer.send("Messages", message, sizeof(message));

Messages from the remote peer are received by the onPeerMessage handler:

config.onPeerMessage = [this](const auto peer, const auto channel, const auto data, auto len) {
    auto name = peer.externalId();
    std::cout << "Got a message from " << name << ": "
        << std::string(data, data + len) << std::endl;
    onPeerMessage(data, len);