WebSync clients can publish, subscribe, and receive streaming data, among other things. They are intended for full client-side integration.
Insert the following tag in the <head> section of your page:
<script type="text/javascript" src="http://sync.frozenmountain.com/client.js"> </script>
Now you're ready to start coding! Here's a quick sample to get you going:
var client = fm.websync.client; // use your domain key here (if using WebSync On-Demand) client.initialize({ key: '11111111-1111-1111-1111-111111111111' }); client.connect(); client.subscribe({ channel: '/sample/channel', onReceive: function(args) { alert(args.data.text); } }); client.publish({ channel: '/sample/channel', data: { text: 'Hello, world!' } });
One special note: if you're unsure about something related to javascript client, you can always view the full source by simply adding the query string ?debug=1 to the end of your <script> tag, like this.
Use the JavaScript client by calling the public methods
available in fm.websync.client. A client will
typically initialize,
connect, and then
subscribe and
publish.
fm.websync.client.initialize(config);
Initializes the client for use.
config is an object with the following properties:
The config object also supports the following optional parameters that affect how the client connects to WebSync.
Example:
// use your domain key here (if using WebSync On-Demand) fm.websync.client.initialize({ key: '11111111-1111-1111-1111-111111111111', urls: { connect: 'http://mydomain.com/authenticate-proxy.aspx' } });
fm.websync.client.connect(config);
Connects the client to the server and delivers any pending requests.
config is an object with the following properties:
Example:
fm.websync.client.connect({
ext: {
foo: 'bar'
},
onComplete: function() {
alert('connect complete');
},
onSuccess: function(args) {
alert('connect win');
},
onFailure: function(args) {
alert('connect fail');
}
});
fm.websync.client.subscribe(config);
Subscribes the client to a channel or array of channels. Received messages are delivered to the onReceive method. (Note that publishers do not receive their own publications.)
config is an object with the following properties:
Example:
fm.websync.client.subscribe({
channel: '/channel/name',
ext: {
foo: 'bar'
},
onComplete: function() {
alert('subscribe complete');
},
onSuccess: function(args) {
alert('subscribe win');
},
onFailure: function(args) {
alert('subscribe fail');
},
onReceive: function(args) {
alert('receive win');
},
onMetaReceive: function(args) {
alert('meta-receive win');
}
});
fm.websync.client.publish(config);
Publishes data to the subscribers of a channel. The data must be a single serializable object.
config is an object with the following properties:
Example:
fm.websync.client.publish({
channel: '/channel/name',
data: {
blah: 'blah',
test: true,
value: 1.2
},
ext: {
foo: 'bar'
},
onComplete: function() {
alert('publish complete');
},
onSuccess: function(args) {
alert('publish win');
},
onFailure: function(args) {
alert('publish fail');
}
});
fm.websync.client.unsubscribe(config);
Unsubscribes the client from a channel or array of channels.
config is an object with the following properties:
Example:
fm.websync.client.unsubscribe({
channel: '/channel/name',
ext: {
foo: 'bar'
},
onComplete: function() {
alert('unsubscribe complete');
},
onSuccess: function(args) {
alert('unsubscribe win');
},
onFailure: function(args) {
alert('unsubscribe fail');
}
});
fm.websync.client.disconnects(config);
Terminates the connection to the server. A call to connect() can re-open the connection.
config is an object with the following properties:
Example:
fm.websync.client.disconnect({
ext: {
foo: 'bar'
},
onComplete: function() {
alert('disconnect complete');
},
onSuccess: function(args) {
alert('disconnect win');
},
onFailure: function(args) {
alert('disconnect fail');
}
});
The JavaScript client has some additional methods that don't affect its state, but instead provide useful information for your applications.
fm.websync.client.getClientId();
Returns the client's unique identifier, or 0 if it has not connected.
fm.websync.client.getClientId(); // returns client id
The JavaScript client has some helpful utility methods that can be used
in your own application. They all reside in fm.websync.utilities.
fm.websync.utilities.extend(dest, src);
Extends destination object by copying all properties from source object.
var dest = { foo: 'bar', test: true }; var src = { value: 2 test: false }; fm.websync.utilities.extend(dest, src); // dest is now { foo: 'bar', value: 2, test: false }
fm.websync.utilities.isArray(element);
Detects whether or not an element is an array.
var arr = [ 1, 2, 3 ]; var obj = { foo: 'bar' }; fm.websync.utilities.isArray(arr); // returns true fm.websync.utilities.isArray(obj); // returns false
fm.websync.utilities.isString(element);
Detects whether or not an element is a string.
var arr = [ 1, 2, 3 ]; var obj = { foo: 'bar' }; fm.websync.utilities.isArray("test"); // returns true fm.websync.utilities.isArray({}); // returns false
fm.websync.utilities.json.parse(str);
Parses a JSON string into a native JavaScript object.
var str = '{"foo":"bar"}'; var obj = fm.websync.utilities.json.parse(str); // obj is { foo: 'bar' }
fm.websync.utilities.json.stringify(obj);
Serializes a native JavaScript object into a JSON string.
var obj = { foo: 'bar' }; var str = fm.websync.utilities.json.stringify(obj); // str is '{"foo":"bar"}'
fm.websync.utilities.log(data, force);
Logs data to the console (or the current document body if console is unavailable).
// use force=true to override global debug value fm.websync.utilities.log('Hello, world', true);
fm.websync.utilities.observe(element, event, handler);
Attaches an event handler to a DOM element.
fm.websync.utilities.observe(window, 'load', function() { alert('Window loaded!'); });
A simple demonstration is often the best way to learn.
Put the following code in an HTML file and open it in multiple browser tabs/windows. Each time a copy loads, it will publish a simple message to any other loaded pages.
<html> <head> <title>Hello World</title> <script type="text/javascript" src="http://sync.frozenmountain.com/client.js"></script> </head> <body> <script type="text/javascript"> var client = fm.websync.client; // initialize the client // use your domain key here (if using WebSync On-Demand) client.initialize({ key: '11111111-1111-1111-1111-111111111111' }); // connect to the server client.connect({ ext: { userName: 'jsmith' } }); // subscribe the client and set up our receive handler client.subscribe({ channel: '/test', onReceive: function(args) { var div = document.createElement('div'); div.innerHTML = args.data.text; document.body.appendChild(div); } }); // publish some data client.publish({ channel: '/test', data: { text: 'Hello, world!' } }); </script> </body> </html>
For each client method, the optional url argument allows you
to redirect the method requests to a different URL. This is known as
"proxying" and allows pre-processing of messages on your server. It is useful for
integrating custom functionality and business logic, such as authentication,
persistence, authorization, contact list management, etc. For more information,
see Proxies.
If you haven't already:
You're ready to start using the client!
To create a client, simply instantiate an instance of Client:
// WebSync On-Demand Only Client client = new Client( "11111111-1111-1111-1111-111111111111", // replace with your domain key "mydomain.com" // replace with your domain name ); // WebSync Server Only Client client = new Client( "http://mydomain.com/request.ashx" // replace with your request handler );
After creating a new client:
ConnectCompleted event for post-connect operations.
Receive event for receiving incoming messages.
Connect to kick off the process.
Once the ConnectCompleted handler has raised, you can make calls
to the other client methods. A typical application will Subscribe
and Publish. Incoming
messages will raise the Receive handler.
All properties and methods are fully documented in the library.
static void Main(string[] args) { // create the client // use your domain key and name here (if using WebSync On-Demand) Client client = new Client( "11111111-1111-1111-1111-111111111111", "mydomain.com" ); // attach the event handlers client.ConnectCompleted += new EventHandler<ConnectCompletedEventArgs>(ConnectCompleted); client.Receive += new EventHandler<ReceiveEventArgs>(Receive); // connect to the server client.Connect(); } void ConnectCompleted(object sender, ConnectCompletedEventArgs e) { // subscribe the client e.Client.Subscribe("/test"); // publish some data e.Client.Publish(new Publication() { Channel = "/test", Data = "{\"text\":\"Hello, world!\"}" // must be valid JSON }); } void Receive(object sender, ReceiveEventArgs e) { // dump the data to the console (in JSON) Console.WriteLine(e.Data); }
For each client method, the optional proxy parameter allows you
to redirect the method requests to a different URL. This is known as
"proxying" and allows custom message pre-processing. It is useful for
integrating custom functionality and business logic, such as authentication,
persistence, authorization, contact list management, etc. For more information,
see Proxies.