WebSync Tutorials - Frozen Mountain

WebSync 3 Tutorials


WebSync On-Demand Tutorials WebSync Server Tutorials

WebSync On-Demand: Publishers

Publishers let you publish data without the overhead of maintaining an active client connection. They can be invoked from almost anywhere in code and are a fast way to push server-side data to clients. In this tutorial, you will create a page that publishes data each time it is loaded.

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 as part of 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 publisher

A publisher can send messages from anywhere in managed code. In this case, we're going to publish from the code-behind of an empty aspx page.

Markup:

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

Code-Behind:

namespace Tutorials
{
    public partial class Publish : System.Web.UI.Page
    {
        [DataContract]
        private class Data
        {
            [DataMember(Name = "text")]
            public string Text { get; set; }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            // create the publisher
            Publisher publisher = new Publisher(new PublisherArgs
            {
                DomainKey = "11111111-1111-1111-1111-111111111111", // your public or private key
                DomainName = "localhost"                            // your domain
            });
            
            // publish our data (note the channel matches the other examples)
            Publication publication = publisher.Publish("/test", JSON.Serialize(new Data()
            {
                Text = "Hello, world!"
            }));
            
            // process the response
            if (publication.Successful == true)
            {
                Response.Write("Success");
            }
            else
            {
                Response.Write("Failure");
            }
        }
    }
}

Testing

Open up your client page in one window. Load this new page in another to see the publisher in action!