| WebSync On-Demand Tutorials | WebSync Server Tutorials |
|---|---|
|
|
|
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.
Before you can start coding, you need to have the correct project references.
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");
}
}
}
}
Open up your client page in one window. Load this new page in another to see the publisher in action!