Message Relay

Sending relay messages is similar to RPCs but they don’t require a network object as a target; the messages are sent directly to the clients. Unlike RPCs, you can only send one argument that is passed as an object, so if you want to send several arguments, you have to pack them in a container object.

Sending Relay Messages

There are two method groups for sending relay messages:

  1. Broadcast

    By using these methods, you can send messages to all the room members simultaneously.

    void SendRoomRelay(object messageToRelay, RoomRelayEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
    
    void SendRoomRelay(RoomRelayMessage message, RoomRelayEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
    

    This overload allows you to specify the ID of the room you’re sending the message to, in addition to the message itself.

    Note

    You cannot send relays outside of your room.

  2. Direct

    These methods allow you to send messages to only one room member. To specify the recipient, you have to pass their UID as the first argument.

    There are several ways to get the UID of the room member.

    1. The easiest way is to find a game object that belongs to that member, get the StrixReplicator component from it, and use this component’s ownerUid property.

    2. If someone sends you an RPC or a relay message, you can get the UID of the sender using the StrixRpcContext’s senderUid property in case of RPCs, or the GetFromUid() method of the relay notification.

    3. You can iterate over all room members using the StrixNetwork.instance.roomMembers property and choose the one you need based on criteria.

    void SendRoomDirectRelay(UID to, object messageToRelay, RoomDirectRelayEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
    
    void SendRoomDirectRelay(RoomDirectRelayMessage message, RoomDirectRelayEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
    

    This overload allows you to specify the ID of the room you’re sending the message to, in addition to the ID of the recipient and the message itself.

    Note

    You cannot send relay messages outside of your room.

    Note

    UID is an interface type. You can’t compare two UIDs by == operator.

Receiving Relay Messages

On the receiving end, you have to subscribe to relay message notification events:

// Broadcast messages
StrixNetwork.instance.roomSession.roomClient.RoomRelayNotified

// Direct messages
StrixNetwork.instance.roomSession.roomClient.RoomDirectRelayNotified

The notification handler has one argument of type NotificationEventArgs<RoomRelayNotification> or NotificationEventArgs<RoomDirectRelayNotification>. This object has a property called Data through which you can access the received message and the UID of the room member who sent it.

StrixNetwork.instance.roomSession.roomClient.RoomDirectRelayNotified += notification => {
    Debug.Log("Received a new relay message " + notification.Data.GetMessageToRelay() + " from " + notification.Data.GetFromUid());
}

Message Contents

Primitive values

If your message is just a primitive type like int, you can send it as is:

StrixNetwork.instance.SendRoomRelay(
    messageToRelay: 10,
    handler: null,
    failureHandler: null
);

Custom messages

If you want to send several primitive types, you’ll have to define and register a class to contain them. Here’s an example:

class MyCustomMessage
{
    public int Health;
    public string Name;
}

After you’ve defined your class, you have to register it by calling ObjectFactory.Instance.Register with the type of your message class:

ObjectFactory.Instance.Register(typeof(MyCustomMessage));

These steps should be carried out both on the client that sends the message and on all the clients receiving it.

Now you can use your class as a relay message:

StrixNetwork.instance.SendRoomRelay(
    messageToRelay: new MyCustomMessage {
        Health = 100,
        Name = "Stranger"
    },
    handler: null,
    failureHandler: null
)

Note

ObjectFactory is a Strix’s class declared in SoftGear.Strix.Net.Serialization namespace. You will need the following using directive in your script:

using SoftGear.Strix.Net.Serialization;

Note

The custom message class should be registered before joining a room. If another client sends you a relay message of a custom message class before registering the class, a Strix Error of “Message Deserialize failed” appears in the Unity log, even if you have not subscribed any relay message notification event (yet).

Serialization

In general, relay messages use the same serialization as RPCs, so you can refer to the Serialization section of RPCs.