WebSync: Documentation
WebSync publishers can publish data without having to maintain an active
connection. They are intended for server-side integration.
.NET Publisher
Getting Started
If you haven't already:
- Download the latest copy of WebSync On-Demand .NET.
- Add a reference to the downloaded library to your project.
You're ready to start coding!
Using the Publisher
To create a publisher, simply instantiate an instance of
Publisher:
// WebSync On-Demand Only Publisher publisher = new Publisher( "11111111-1111-1111-1111-111111111111", // replace with your domain key "mydomain.com" // replace with your domain name ); // WebSync Server Only Publisher publisher = new Publisher( "http://mydomain.com/request.ashx" // replace with your request handler );
For WebSync Server, direct publishing over HTTP must be enabled server-side.
After creating a new publisher, make calls to
Publish as
necessary. The outgoing string parameter of Publish is empty
if all publications were successful. It contains an array of JSON objects
with successful and error properties if at least
one of the publications failed.
Example
// create the publisher // use your domain key and name here (if using WebSync On-Demand) Publisher publisher = new Publisher( "11111111-1111-1111-1111-111111111111", "mydomain.com" ); // publish data string result; bool success = client.Publish(new Publication[] { new Publication() { Channel = "/test", Data = "{\"text\":\"Hello, world!\"}" // must be valid JSON }, new Publication() { Channel = "/test", Data = "{\"text\":\"Hello, universe!\"}" // must be valid JSON } }, out result);
PHP Publisher
Getting Started
If you haven't already:
- Download the latest copy of WebSync On-Demand PHP.
- Include a reference to 'Publisher.php' in your script.
You're ready to start coding!
Using the Publisher
To create a publisher, simply instantiate an instance of
Publisher:
// WebSync On-Demand Only $publisher = new Publisher( "11111111-1111-1111-1111-111111111111", // replace with your domain key "mydomain.com" // replace with your domain name ); // WebSync Server Only $publisher = new Publisher( "http://mydomain.com/request.ashx" // replace with your request handler );
After creating a new publisher, make calls to
publish,
passing your publication(s). The string returned the method is empty
if all publications were successful. It contains an array of JSON objects
with successful and error properties if at least
one of the publications failed.
The
publish method takes either a single publication or
an array of publications. Each publication is an associative array with
channel and data properties:
-
channel (string)
The name of the channel to which the client should publish. -
data (object)
The data to publish (a single serializable object).
Example
// create the publisher // use your domain key and name here (if using WebSync On-Demand) $publisher = new Publisher( "11111111-1111-1111-1111-111111111111", "mydomain.com" ); // publish data $response = $publisher->publish(array( array( 'channel' => '/test', 'data' => (object) array( 'text' => 'Hello, world!' ) ), array( 'channel' => '/test', 'data' => (object) array( 'text' => 'Hello, universe!' ) ) )); // success if empty (no error) $success = empty($response);
HTTP Publishing
It is possible to publish from any language with HTTP support (.NET, Java,
PHP, Python, Ruby, etc.) by sending a GET or POST request.
Using GET
Publish messages by sending an HTTP request with a method type of GET to:
// WebSync On-Demand Only http://sync.frozenmountain.com/request.ashx?key=KEY&publications=PUBLICATIONS // WebSync Server Only (replace with your request handler) http://mydomain.com/request.ashx?publications=PUBLICATIONS
-
For WebSync On-Demand only, replace
KEYwith your public or private domain key. -
Replace
PUBLICATIONSwith a URL-encoded array of publications in JSON format. -
Set the referrer of the GET request to your fully-qualified domain,
including an
http://prefix.
Be cautious of size limitations! Local proxies often enforce a limit
on the query string length and may truncate your publications. If your
submission is larger than 1-2 KB, we recommend using POST instead.
C# Example:
// prepare our publications in valid JSON string publications = HttpUtility.UrlEncode( "[{\"channel\":\"/test\",\"data\":{\"text\":\"Hello, world!\"}}]" ); // create the request // use your domain key here (if using WebSync On-Demand) HttpWebRequest request = (HttpWebRequest)WebRequest.Create( "http://sync.frozenmountain.com/request.ashx" + "?key=11111111-1111-1111-1111-111111111111" + "&publications=" + publications ); // set request options // use your domain here (if using WebSync On-Demand) request.Referer = "http://mydomain.com"; request.Method = "GET"; // get response string response = ""; using (Stream respStream = request.GetResponse().GetResponseStream()) using (StreamReader respReader = new StreamReader(respStream)) response = respReader.ReadToEnd(); // success if empty (no error) bool success = (string.IsNullOrEmpty(response));
PHP Example:
// prepare our publications in valid JSON $publications = urlencode( "[{\"channel\":\"/test\",\"data\":{\"text\":\"Hello, world!\"}}," + "{\"channel\":\"/test\",\"data\":{\"text\":\"Hello, universe!\"}}]" ); // create the request // use your domain key here (if using WebSync On-Demand) $ch = curl_init( "http://sync.frozenmountain.com/request.ashx" + "?key=11111111-1111-1111-1111-111111111111" + "&publications=$publications" ); // set request options // use your domain here (if using WebSync On-Demand) curl_setopt($ch, CURLOPT_REFERER, "http://mydomain.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get response $response = curl_exec($ch); curl_close($ch); // success if empty (no error) $success = empty($response);
Using POST
Publish messages by sending an HTTP request with a method type of POST to:
// WebSync On-Demand Only http://sync.frozenmountain.com/request.ashx?key=KEY // WebSync Server Only (replace with your request handler) http://mydomain.com/request.ashx
-
For WebSync On-Demand only, replace
KEYwith your public or private key. -
Set the content-type to
application/json. -
Set the referrer of the POST request to your fully-qualified domain,
including an
http://prefix.
POST is more reliable for large data publications as it does not have the size limitations of GET.
C# Example:
// prepare our publications in valid JSON string publications = "[{\"channel\":\"/test\",\"data\":{\"text\":\"Hello, world!\"}}]"; // create the request // use your domain key here (if using WebSync On-Demand) HttpWebRequest request = (HttpWebRequest)WebRequest.Create( "http://sync.frozenmountain.com/request.ashx" + "?key=11111111-1111-1111-1111-111111111111" ); // set request options // use your domain here (if using WebSync On-Demand) request.Referer = "http://mydomain.com"; request.Method = "POST"; request.ContentType = "application/json"; // write POST data using (Stream reqStream = request.GetRequestStream()) using (StreamWriter reqWriter = new StreamWriter(reqStream)) reqWriter.Write(publications) // get response string response = ""; using (Stream respStream = request.GetResponse().GetResponseStream()) using (StreamReader respReader = new StreamReader(respStream)) response = respReader.ReadToEnd(); // success if empty (no error) bool success = (string.IsNullOrEmpty(response));
PHP Example:
// prepare our publications in valid JSON $publications = "[{\"channel\":\"/test\",\"data\":{\"text\":\"Hello, world!\"}}," + "{\"channel\":\"/test\",\"data\":{\"text\":\"Hello, universe!\"}}]"; // create the request // use your domain key here (if using WebSync On-Demand) $ch = curl_init( "http://sync.frozenmountain.com/request.ashx" + "?key=11111111-1111-1111-1111-111111111111" ); // set request options // use your domain here (if using WebSync On-Demand) curl_setopt($ch, CURLOPT_REFERER, "http://mydomain.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); // write POST data curl_setopt($ch, CURLOPT_POSTFIELDS, $publications); // get response $repsonse = curl_exec($ch); curl_close($ch); // success if empty (no error) $success = empty($response);
