Connection to signaling

This article explains how to connect a client to signaling and join a room via VT API

To connect you need to know the values of the following fields from the user license:signaling_url,username,password - you can get them from the license file with those credentials.

JSON query example:
query
{
  "username": "john_doe@medialooks.com",  // username from license
  "auth": "{\"password\":\"qwerty\"}"     // password from license
}
C# code example:
public void Connect(string signaling, string room, string username, string password)
        {
            _room = room;

            SocketIO = new SocketIO(signaling.StartsWith("https") ? signaling : $"https://{signaling}");

            SocketIO.Options.Query = new Dictionary<string, string>
            {
                ["username"] = username,
                ["auth"] = $"{{\\"password\\":\\"{password}\\"}}",
            };
            Console.WriteLine($"--> query: {SocketIO.JsonSerializer.Serialize(new object[] { SocketIO.Options.Query }).Json}");

            SocketIO.OnError += (_, e) => OnError(e);
            SocketIO.OnConnected += (_, __) => Console.WriteLine("<-- connected");
            SocketIO.OnDisconnected += (_, __) => Console.WriteLine("<-- disconnected");
            SocketIO.OnAny(EventHandler);
            SocketIO.ConnectAsync();
        } 

You will need to handle the following events:

  • on_connection, you will need auth_id value from this event response.

  • on_join

  • on_leave

JSON on_connectionexample:
on_connection
[
  {
    "session_id": 1653558286392,
    "signaling_ver": "2.1",
    "remoteAddress": "::ffff:172.18.0.1",
    "remotePort": 38822,
    "headers": {...},
    "server_metadata": {...},
    "username": "test@medialooks.com",
    "role": "owner",
    "auth_room": "Auth",
    "auth_id": "pol2b6VKbaSVbCyyAAIs"
  }
]
JSON on_join example:
on_join (first value - id of joined member)
[
  "EawLi6fQ9jKByY/R46TRSyVHsKY=",    // joined member id
  {
    "hardware_id": "{42CE8DCF-14EC-00DA-9625-91768E3ED46E}",
    "location": "US, Chicago",
    "mode": "enum",
    "name": "_VT_MNG_RCV_",
    "peerType": "cpp",
    "rcv_name": "VT_ReceiverMng",
    "vt_version": "1.9.4.966",
    "id": "WbaqzsMXqeQiwuoa",
    "private": {...},
    "join_props": {...}
  }
]
JSON on_join example:
on_leave (first value - id of leaved member)
[
  "EawLi6fQ9jKByY/R46TRSyVHsKY=",    // left member id
  {
    "hardware_id": "{42CE8DCF-14EC-00DA-9625-91768E3ED46E}",
    "location": "US, Chicago",
    "mode": "enum",
    "name": "_VT_MNG_RCV_",
    "peerType": "cpp",
    "rcv_name": "VT_ReceiverMng",
    "vt_version": "1.9.4.966",
    "id": "VCLNo00uM7plb9rH",
    "private": {...},
    "join_props": {...}
  }
]
C# code example:
 private void EventHandler(string @event, SocketIOResponse response)
        {
            try
            {
                Console.WriteLine($"<-- {@event}: {response}");
                switch (@event)
                {
                    case "on_connection":
                        _authId = response.GetValue(0).GetProperty("auth_id").GetString();
                        RequestRoomId();
                        break;

                    case "on_join":
                        var joined = response.GetValue(1).Deserialize<Manager>();
                        if (joined.Mode == "enum" && (joined.Name == "_VT_MNG_PBL_" || joined.Name == "_VT_MNG_RCV_"))
                        {
                            joined.Client = this;
                            Managers.Add(joined);
                        }
                        break;

                    case "on_leave":
                        var leaved = response.GetValue(1).Deserialize<Manager>();
                        if (Managers.FirstOrDefault(m => m.Id == leaved.Id) is Manager manager)
                        {
                            Managers.Remove(manager);
                        }
                        break;
                }
            }

Join room

To join the room you will need the room_id which is generated by the signaling after you send the auth_room_name_mask like in the following JSON request.

JSON request example:
request
{
"to": "CCMXU5yhu4M5wvDkAAAB",    // auth_id from on_connection
"type": "auth_room_name_mask",
"payload": {
"room_name": "john_doe_room"     // publisher_id from the license
}
}

In response, you will get the callback with the room_id.

JSON callback example:
callback
[
null,    // error if occured
{
"room_id": "EawLi6fQ9jKByY/R46TRSyVHsKY="
}
]

C# Code example:

C# code example:
private void RequestRoomId()
        {
            var request = new
            {
                to = _authId,
                type = "auth_room_name_mask",
                payload = new
                {
                    room_name = _room,
                },
            };

            Action<SocketIOResponse> callback = (response) =>
            {
                Console.WriteLine($"<-- response: {response}");

                try
                {
                    if (response.GetValue<Error>(0) == null)
                    {
                        _roomId = response.GetValue(1).GetProperty("room_id").GetString();
                        JoinRoom();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"exception: {e.Message}");
                }
            };

            Emit("request", request, callback);
        }

Now when you get room_id value you can join room:

JSON join example:
join
"EawLi6fQ9jKByY/R46TRSyVHsKY="    // room_id from auth_room_name_mask response
C# code example:
private void JoinRoom()
        {
            Action<SocketIOResponse> callback = (response) =>
            {
                Console.WriteLine($"<-- response: {response}");

                try
                {
                    if (response.GetValue<Error>(0) == null)
                    {
                        var managers = response.GetValue(1)
                                               .GetProperty("members")
                                               .EnumerateObject()
                                               .Select(p => p.Value.Deserialize<Manager>())
                                               .Where(m => m.Mode == "enum" && (m.Name == "_VT_MNG_PBL_" || m.Name == "_VT_MNG_RCV_"))
                                               .ToArray();

                        Managers.Clear();
                        foreach (var manager in managers)
                        {
                            manager.Client = this;
                            Managers.Add(manager);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"exception: {e.Message}");
                }
            };

            Emit("join", _roomId, callback);
        }

You will receive a callback with members field - a collection of managers in the room you’ve joined.

JSON callback example:
callback
    [
    null,    // error if occured
    {
        "name": "EawLi6fQ9jKByY/R46TRSyVHsKY=",
        "members": {// room members collection, take only managers(mode == "enum" && (name == "*VT_MNG_PBL*" || name == "*VT_MNG_RCV*"))
        "VCLNo00uM7plb9rH": {
        "hardware_id": "{42CE8DCF-14EC-00DA-9625-91768E3ED46E}",
        "location": "US, Chicago",
        "mode": "enum",
        "name": "*VT_MNG_RCV*",
        "peerType": "cpp",
        "rcv_name": "VT_ReceiverMng",
        "vt_version": "1.9.4.966",
        "id": "VCLNo00uM7plb9rH",    // manager id for further requests
"private": {...},
"join_props": {...}
},
"gBZX5HEiKGqmIMWR": {
"hardware_id": "{42CE8DCF-14EC-00DA-9625-91768E3ED46E}",
"location": "US, Chicago",
"mode": "enum",
"name": "*VT_MNG_PBL*",
"pbl_name": "VT_PublisherMng",
"peerType": "cpp",
"vt_version": "1.9.4.966",
"id": "gBZX5HEiKGqmIMWR",
"private": {...},
"join_props": {...}
},
...
}
}
]

Optionally you can enumerate the managers with an individual request:

C# code example:
public void Refresh()
        {
            Action<SocketIOResponse> callback = (response) =>
            {
                Console.WriteLine($"<-- response: {response}");

                try
                {
                    if (response.GetValue<Error>(0) == null)
                    {
                        var managers = response.GetValue(1)
                                               .GetProperty("members")
                                               .EnumerateObject()
                                               .Select(p => p.Value.Deserialize<Manager>())
                                               .Where(m => m.Mode == "enum" && (m.Name == "_VT_MNG_PBL_" || m.Name == "_VT_MNG_RCV_"))
                                               .ToArray();

                        Managers.Clear();
                        foreach (var manager in managers)
                        {
                            manager.Client = this;
                            Managers.Add(manager);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"exception: {e.Message}");
                }
            };

            Emit("room_members_get", _roomId, callback);
        }

Right now you are prepared to work with managers.

Last updated