Last updated

Bus Client Tutorial

Namespace: Agora.Edge

The BusClient is an integral part of Edge SDK and is implemented as a Singleton. The purpose of the BusClient is to send and receive messages using the NATS broker.

Bus Client

Configuration Parameters:

  • Name (string) - Used to set the NATS Client Name that identifies the client to the NATS server. Although "optional", it is recommended to specify the "Name" setting in the AEA.json configuration file. configuration file.
  • AEA2:BusClient:Server (string): Server URL hosting the NATS Server. Default: nats://localhost:4222.
  • AEA2:BusClient:Subscriptions (string array): Array of string topics to subscribe to grouped by their type. Default: <empty>. Commonly, 'slbapps.<app-name>.DataIn', and 'slbapps.<app-name>.RequestIn' are frequently used.
  • AEA2:BusClient:Targets (string array): Array of string topics to publish to. Targets are required to send messages. Default: <empty>. Commonly, 'slbapps.<app-name>.DataIn', and 'slbapps.<app-name>.RequestIn' are frequently used.

Prior to SDK 3.0, NATS topics which are now listed in the AEA.json configuration file under Subscriptions and Targets were managed by the AEA broker and enumerated in the routes.json configuration file. With SDK 3.0, the AEA Broker and routes.json are no longer used.

Example of AEA.json configuration file:

{
    "Name": "MyApp",
    "AEA2": {
        "RedisClient": {
            "Server": "localhost",
            "Port": "6379"
        },
        "BusClient": {
            "Server": "nats://localhost:4222",
            "Subscriptions": [
                {
                    "DataIn": [
                        "slbapps.MyApp.DataIn",
                        "slbapps.events.DataIn"
                    ]
                },
                {
                    "RequestIn": [
                        "slbapps.MyApp.RequestIn"
                    ]
                },
                {
                    "EventIn": [
                        "slbapps.MyApp.EventIn"
                    ]
                }
            ],
            "Targets": [
                {
                    "DataOut": [
                        "slbapps.app1.DataIn",
                        "slbapps.app2.DataIn",
                    ]
                },
                {
                    "RequestOut": [
                        "slbapps.app1.RequestIn",
                        "slbapps.app2.RequestIn",
                    ]
                },
                {
                    "EventOut": [
                        "slbapps.app1.EventIn",
                        "slbapps.app2.EventIn",
                    ]
                }
            ]
        }
    }
}

Properties:

  • Instance: BusClient - Used to access the Singleton instance and is the same as using the Agora.SDK.BusClient.
  • IsConnected: bool - True if connected.
  • Messages: BusMessageQueues - Provides access to the queues used to store incoming messages.

Methods:

  • void Connect(int timeout_msec) - Connects to the Bus using configuration settings.
  • bool IsConnected() - Returns true if BusClient is connected.
  • void SendData(IoDataReportMsg data, string msgTopic = "DataOut") - Sends an IoDataReportMsg to the Bus. Messages are sent to the topic, msgTopic which defaults to 'DataOut' list in Targets.
  • void SendRequest(RequestMsg request, string msgTopic = "RequestOut") - Sends a RequestMsg to the Bus. Messages are sent to the topic, msgTopic which defaults to 'RequestOut' list in Targets.
  • void SendMessage(string topic, MessageHeader header, string jsonMessage) - Sends a json message as json "payload" element and header as json "header" element using the specified topic.
  • async Task<bool> SendRawMessage(string topic, string message) - Sends a raw message as a string to the specified topic.
  • void SendEvent(EventMsg eventmsg, string msgTopic = "EventOut") - Sends a EventMsg to the Bus. Messages are sent to the topic, msgTopic which defaults to 'EventOut' list in Targets.
  • connect(int timeout_sec) - Connects to the Bus using configuration settings.
  • is_connected() - Returns true if BusClient is connected.
  • send_data(data: IoDataReportMsg, msgTopic = "DataOut": str) - Sends an IoDataReportMsg to the Bus. Messages are sent to the topic, msgTopic which defaults to 'DataOut' list in Targets..
  • send_request(request: RequestMsg, msgTopic = "RequestOut": str) - Sends a RequestMsg to the Bus. Messages are sent to the topic, msgTopic which defaults to 'RequestOut' list in Targets.
  • void send_message(topic: str, header: MessageHeader, jsonMessage: str) - Sends a json message as json "payload" element and header as json "header" element using the specified topic.
  • void send_raw_message(topic: str, message: str) - Sends a raw message as a string to the specified topic.
  • send_event(event: EventMsg, msgTopic = "EventOut": str) - Sends a EventMsg to the Bus. Messages are sent to the topic, msgTopic which defaults to 'EventOut' list in Targets.