You can broadcast more complex messages by serializing the payload before you publish it:
// definition of a message
public class ChatMessage
{
public Guid Id { get; set; }
public string User { get; set; }
public string Text { get; set; }
}
// grab an instance of an ISubscriber
var subscriber = connection.GetSubscriber();
var message = new ChatMessage
{
Id = Guid.NewGuid(),
User = "User 1234",
Text = "Hello World!"
};
// serialize a ChatMessage
// this uses JIL to serialize to JSON
var json = JSON.Serialize(message);
// publish the message to the 'chat' channel
subscriber.Publish("chat", json)
The subscriber then needs to deserialize the message:
// grab an instance of an ISubscriber
var subscriber = connection.GetSubscriber();
// subscribe to messages over the 'chat' channel
subscriber.Subscribe("chat", (channel, json) => {
var message = JSON.Deserialize<ChatMessage>(json);
// do something with the message
Console.WriteLine($"{message.User} said {message.Text}");
});