How do I get chat history when a client connects?

WebSync has no built in concept of chat history, so a common question is how to implement history and use it to "rehydrate" clients when they connect (or reconnect). Building this functionality into your solution is a simple process on the WebSync signalling server. The WebSync server would be responsible for logging messages and loading past messages.

In your server code, add these two methods. One method to write messages to your database, and one method to read and return them when a new client subscribes:

[WebSyncEvent(EventType.BeforePublish)]
public static void LogPublish(object sender, WebSyncEventArgs e)
{
    var message = Json.Deserialize<MyMessage>(e.PublishInfo.DataJson);
    WriteToDatabase(e.PublishInfo.Channel, message); // your function to write to your database
}

[WebSyncEvent(EventType.AfterSubscribe)]
public static void RestoreHistory(object sender, WebSyncEventArgs e)
{
    var pastMessages = ReadFromDatabase(e.SubscribeInfo.Channel); // your function to read from your database
    e.MetaJson = Json.Serialize(pastMessages); // send history back in subscribe response metadata
}


In the client, you can then parse out the history after joining the conference:

client.JoinConference(new JoinConferenceArgs(...)
{
    ...
    OnSuccess = (e) =>
    {
        var pastMessages = Json.Deserialize<Message[]>(e.MetaJson);
        // write the past messages to the UI
    }
});

See here for another example of this approach: http://docs.frozenmountain.com/websync4/#class=websync-code-snippets-loading-initial-data

It works for any case where you want to load up initial data in a channel for new subscribers.