WebSync Tutorials - Frozen Mountain

WebSync 3 Tutorials


WebSync On-Demand Tutorials WebSync Server Tutorials

WebSync Server: Events

In this tutorial you will learn how to attach custom code to WebSync Server events. We will use a simple publish hook to modify messages on-the-fly.

Prerequisites

  1. You have completed the WebSync Server: Basic tutorial.

Configuring your project

Before you can start coding, you need to have the correct project references.

  1. FM.WebSync.Server (available as part of WebSync Server)
  2. FM.WebSync.Core (available as part of WebSync Server)
  3. FM.WebSync.Core.Json (available as part of WebSync Server)
  4. System.Runtime.Serialization (part of the .NET framework)

Attaching to a WebSync event

Methods are attached to WebSync events by adding a simple attribute to the method definition.

We create a generic class to hold the method, define a Data class for the purposes of deserialization, and create a method that will attach to the BeforePublish event.

// a generic class that contains our WebSync events
public class WebSyncEvents
{
    [DataContract]
    private class Data
    {
        [DataMember(Name = "text")]
        public string Text { get; set; }
    }

    [WebSyncEvent(EventType.BeforePublish)]
    public static void UpperCasePublishes(object sender, WebSyncEventArgs e)
    {
        foreach (Message message in e.Messages)
        {
            Data data = JSON.Deserialize<Data>(message.DataJson);
            data.Text = data.Text.ToUpperInvariant();
            message.DataJson = JSON.Serialize(data);
        }
    }
}

Testing

Open up your client page in a couple windows and watch as the publications are upper-cased before delivery!