Talo Socket
The Talo Socket is the underlying server that powers services like channels and handles connections, messages and events.
You can learn more about how the socket works here.
Usage
Ideally you should never need to use the socket directly because individual services like players and channels should send the correct data on your behalf. However, the Talo Socket exposes a few public methods and signals for custom logic like handling errors and re-connecting to the server if the connection is closed.
The socket connection is automatically established (this can disabled by setting autoConnectSocket to false in your config). When a player gets identified, they also get automatically identified with the socket server.
Receiving messages
The OnMessageReceived event provides you with a response and payload. For example, if you were building a chat system, you would connect a function similar to the one below to listen for new chat messages:
private void Start()
{
Talo.Socket.OnMessageReceived += OnMessageReceived;
}
private void OnDestroy()
{
Talo.Socket.OnMessageReceived -= OnMessageReceived;
}
private void OnMessageReceived(SocketResponse response)
{
if (response.GetResponseType() == "v1.channels.message")
{
var data = response.GetData<ChannelMessageResponse>();
if (data.channel.id == activeChannelId)
{
AddMessage($"[{data.channel.name}] {data.playerAlias.identifier}: {data.message}");
}
}
}
Sending messages
Generally, sending messages is handled by functions in services like Talo.Channels.SendMessage() where the correct data is prepared and sent for you. You can also use the Talo.Socket.Send() function to send your own requests:
public void SendMessage(int channelId, string message)
{
Talo.IdentityCheck();
var payload = new ChannelMessageRequest
{
channel = new ChannelMessageRequest.ChannelStub { id = channelId },
message = message
};
Talo.Socket.Send(new SocketRequest<ChannelMessageRequest>("v1.channels.message", payload));
}
Handling connection closures
The socket server can disconnect for a number of reasons such as the player going offline or being rate limited. The socket will invoke a OnConnectionClosed event with a status code and reason.
The Talo socket automatically reconnects when the connection is restored. If you need to manually re-open the socket, you can establish a connection and re-identify the player like this:
private async void ReconnectSocket()
{
await Talo.Socket.ResetConnection();
if (Talo.CurrentPlayer != null)
{
var socketToken = await Talo.Players.CreateSocketToken();
Talo.Socket.SetSocketToken(socketToken);
}
}
Closing the connection
You can choose to manually end the socket connection using Talo.Socket.CloseConnection().
Error handling
The Talo Socket exposes an OnErrorReceived event that invokes when a v1.error response is received. You can check the error code (using the SocketErrorCode enum), message and original request through the TaloSocketError object that is sent with the signal:
Talo.Socket.OnErrorReceived += (SocketError err) => {
try {
err.Throw();
} catch (SocketException ex) {
Debug.Log($"Socket error: {ex.Req} - {ex.ErrorCode}{(string.IsNullOrEmpty(ex.Cause) ? "" : " - " + ex.Cause)}");
switch (ex.ErrorCode) {
case SocketErrorCode.NO_PLAYER_FOUND:
Debug.LogError("Player not identified yet!");
break;
case SocketErrorCode.RATE_LIMIT_EXCEEDED:
Debug.LogError("Rate limited!");
break;
}
}
};