Class: Messaging

ClearBlade. Messaging

new Messaging(options, callback)

Initializes the ClearBlade messaging object and connects to a server.
Parameters:
Name Type Description
options Object This value contains the config object for connecting. A number of reasonable defaults are set for the option if none are set.

The connect options and their defaults are:

{number} [timeout] sets the timeout for the websocket connection in case of failure. The default is 60

{Messaging Message} [willMessage] A message sent on a specified topic when the client disconnects without sending a disconnect packet. The default is none.

{Number} [keepAliveInterval] The server disconnects if there is no activity for this pierod of time. The default is 60.

{boolean} [cleanSession] The server will persist state of the session if true. Not avaliable in beta.

{boolean} [useSSL] The option to use SSL websockets. Default is false for now.

{object} [invocationContext] An object to wrap all the important variables needed for the onFalure and onSuccess functions. The default is empty.

{function} [onSuccess] A callback to operate on the result of a sucessful connect. In beta the default is just the invoking of the `callback` parameter with the data from the connection.

{function} [onFailure] A callback to operate on the result of an unsuccessful connect. In beta the default is just the invoking of the `callback` parameter with the data from the connection.

{Object} [hosts] An array of hosts to attempt to connect too. Sticks to the first one that works. The default is ["platform.clearblade.com"].

{Object} [ports] An array of ports to try, it also sticks to thef first one that works. The default is [1337].

callback function Callback to be run upon either succeessful or failed connection
Source:
Example

A standard connect

var callback = function (data) {
  console.log(data);
};
//A connect with a nonstandard timeout
var cb = new ClearBlade.Messaging({"timeout":15}, callback);

Methods

Disconnect()

Disconnects from the server.
Source:
Example

How to publish

var callback = function (data) {
  console.log(data);
};
var cb = ClearBlade.Messaging({}, callback);
cb.Disconnect()//why leave so soon :(

Publish(topic, payload)

Publishes to a topic.
Parameters:
Name Type Description
topic string Is the topic path of the message to be published. This will be sent to all listeners on the topic. No default.
payload string | ArrayBuffer The payload to be sent. Also no default.
Source:
Example

How to publish

var callback = function (data) {
  console.log(data);
};
var cb = ClearBlade.Messaging({}, callback);
cb.Publish("ClearBlade/is awesome!","Totally rules");
//Topics can include spaces and punctuation  except "/" 

Subscribe(topic, options, messageCallback)

Subscribes to a topic
Parameters:
Name Type Argument Description
topic string The topic to subscribe to. No default.
options Object <optional>
The configuration object. Options:

{Number} [qos] The quality of service specified within MQTT. The default is 0, or fire and forget.

{Object} [invocationContext] An object that contains variables and other data for the onSuccess and failure callbacks. The default is blank.

{function} [onSuccess] The callback invoked on a successful subscription. The default is nothing.

{function} [onFailure] The callback invoked on a failed subsciption. The default is nothing.

{Number} [timeout] The time to wait for a response from the server acknowleging the subscription.

messageCallback function Callback to invoke upon message arrival
Source:
Example

How to publish

var callback = function (data) {
  console.log(data);
};
var cb = ClearBlade.Messaging({}, callback);
cb.Subscribe("ClearBlade/is awesome!",{});

Unsubscribe(topic, options)

Unsubscribes from a topic
Parameters:
Name Type Argument Description
topic string The topic to subscribe to. No default.
options Object <optional>
The configuration object

Source:
Example

How to publish

var callback = function (data) {
  console.log(data);
};
var cb = ClearBlade.Messaging({}, callback);
cb.Unsubscribe("ClearBlade/is awesome!",{"onSuccess":function(){console.log("we unsubscribe");});