WebSync Tutorials - Frozen Mountain

WebSync 3 Tutorials


WebSync On-Demand Tutorials WebSync Server Tutorials

WebSync On-Demand: Proxies

Want more control over your messages? In this tutorial, you will learn how our pre-built proxies can be used to intercept messages, read them, and change them. The sky is the limit with what can be done!

Prerequisites

  1. You have completed the WebSync On-Demand: Basic tutorial.

Configuring your project

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

  1. FM.WebSync.Core (available in the WebSync On-Demand SDK)
  2. FM.WebSync.Core.Json (available in the WebSync On-Demand SDK)
  3. System.Runtime.Serialization (part of the .NET framework)

Setting up a proxy

A proxy is essentially an HTTP endpoint that relays messages to and from WebSync On-Demand. A simple way to create this endpoint is to add a blank aspx page and invoke the Proxy class in the code-behind.

Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyProxy.aspx.cs" Inherits="Tutorials.MyProxy" %>

Code-Behind:

namespace Tutorials
{
    public partial class MyProxy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Proxy.Invoke(new ProxyInvokeArgs()
            {
                DomainKey = "22222222-2222-2222-2222-222222222222", // your *private* key
                OnBeforeProxy = (args) =>
                {
                    // handle the requests here
                }
            });
        }
    }
}

Right now, the proxy just relays requests without modifications. We'll make it more interesting in a moment.

Configuring the client

The JavaScript client has to be configured to target the proxy. In this example, we're interested in proxying publications.

Modify the HTML page created in the WebSync On-Demand: Basic tutorial by adding requestUrl.

client.publish({
    requestUrl: '/myproxy.aspx',
    ...
});

Yes, it's that easy. Now we have to make the proxy do something useful.

Modifying messages in the proxy

In this example, we're going to take the incoming text and make it upper-case.

We define a Data class for the purpose of deserialization and in the proxy callback, iterate over the messages and modify the Data property.

[DataContract]
private class Data
{
    [DataMember(Name = "text")]
    public string Text { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    Proxy.Invoke(new ProxyInvokeArgs()
    {
        DomainKey = "11111111-1111-1111-1111-111111111111", // replace with your private key
        OnBeforeProxy = (args) =>
        {
            foreach (Message message in args.Messages)
            {
                // handle the messages here
                if (message.IsPublish())
                {
                    // deserialize, modify, then reserialize the data
                    Data data = JSON.Deserialize<Data>(message.DataJson);
                    data.Text = data.Text.ToUpperInvariant();
                    message.DataJson = JSON.Serialize(data);
                }
            }
        }
    });
    
}

Testing

Open the page in a couple browser windows. Watch as the publications are upper-cased before delivery!