Event-based Publishing and Subscribing

Overview

In this tutorial, we will learn how to use Event Triggers to publish and un-publish a stream, as well as subscribing and un-subscribing to a stream. We will use buttons as examples of triggers, as they are the easiest to demo.

Controlling publishing with event triggers

Starting with a basic 3D scene, add an McPublisher to our Main Camera:

Do not forget to add your Credentials, Stream Name, and make sure **NOT** to tick `Publish On Start` as we will be using events.

Do not forget to add your Credentials, Stream Name, and make sure NOT to tick Publish On Start as we will be using events.

Create two UI Buttons (i.e. from UI > Button - TextMeshPro, call one of them PublishButton and the other UnPublishButton. Also, name their inner Text(TMP) to Publish and Stop Publishing respectively:

Connect the OnClick callback of the PublishButton, to trigger a McPublisher method called Publish. Click on the PublishButton and through the Inspector, look for the OnClick section:

Note: Those methods can also be called from within a script. Please refer to the API Class Documentation to understand how to use it.

Click on the field underneath Runtime Only, and choose the Scene tab. From there, look for Main Camera, which was the object we added our McPublisher to. Make sure to choose whatever you object you added the McPublisher to:

Select Publish in the drop-down menu that says No Function:

Repeat the same steps for the UnPublishButton, and hook it up to the UnPublish method of the McPublisher:

Now, you can run the scene, and test out the buttons.

Adding an event-triggered subscriber

Continue by adding a subscriber to view the stream we are publishing. Add an McSubscriber to any object you like. In this example, we will use the same object we added the publisher to, which is the Main Camera. Make sure to link the Credentials, fill in the stream name with the same name we published with, and add a RawImage to view the incoming stream. Finally, add two buttons, SubscribeButton and UnSubscribeButton so we can hook them up to control our subscriber:

Similar to before, hook the Subscribe button to the Subscribe method of the McSubscriber, and the UnSubscribe Button to UnSubscribe:

You can now control McPublisher and McSubscribe with events.