# 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 [ClearBlade.messagingURI].

  {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:
    - ClearBlade.js, line 1657

  ##### Example

  A standard connect

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

### Methods

#### (static) getAndDeleteMessageHistory(topic, last, count, start, stop, callback)

  Gets the message history from a ClearBlade Messaging topic.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `topic` | string | The topic from which to retrieve history
  `last` | number | Epoch timestamp in seconds that will retrieve and delete 'count' number of messages before that timestamp. Set to -1 if not used
  `count` | number | Number that signifies how many messages to return and delete; 0 returns and deletes all messages
  `start` | number | Epoch timestamp in seconds that will retrieve and delete 'count' number of messages within timeframe. Set to -1 if not used
  `stop` | number | Epoch timestamp in seconds that will retrieve and delete 'count' number of messages within timeframe. Set to -1 if not used
  `callback` | function | The function to be called upon execution of query -- called with a boolean error and the response

  Source:
    - ClearBlade.js, line 1783

#### (static) getMessageHistory(topic, last, count, callback)

  Gets the message history from a ClearBlade Messaging topic.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `topic` | string | The topic from which to retrieve history
  `last` | number | Epoch timestamp in seconds that will retrieve 'count' number of messages before that timestamp. Set to -1 if not used
  `count` | number | Number that signifies how many messages to return; 0 returns all messages
  `callback` | function | The function to be called upon execution of query -- called with a boolean error and the response

  Source:
    - ClearBlade.js, line 1771

#### (static) getMessageHistoryWithTimeFrame(topic, last, count, start, stop, callback)

  Gets the message history from a ClearBlade Messaging topic.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `topic` | string | The topic from which to retrieve history
  `last` | number | Epoch timestamp in seconds that will retrieve 'count' number of messages before that timestamp. Set to -1 if not used
  `count` | number | Number that signifies how many messages to return; 0 returns all messages
  `start` | number | Epoch timestamp in seconds that will retrieve 'count' number of messages within timeframe. Set to -1 if not used
  `stop` | number | Epoch timestamp in seconds that will retrieve 'count' number of messages within timeframe. Set to -1 if not used
  `callback` | function | The function to be called upon execution of query -- called with a boolean error and the response

  Source:
    - ClearBlade.js, line 1742

#### disconnect()

  Disconnects from the server.
  Source:
    - ClearBlade.js, line 1923

  ##### 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:
    - ClearBlade.js, line 1824

  ##### 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, optionsopt, messageCallback)

  Subscribes to a topic

  ##### Parameters:

  Name | Type | Attributes | 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:
    - ClearBlade.js, line 1856

  ##### Example

  How to publish

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

#### unsubscribe(topic, optionsopt)

  Unsubscribes from a topic

  ##### Parameters:

  Name | Type | Attributes | Description
  --- | --- | --- | ---
  `topic` | string |  | The topic to subscribe to. No default.
  `options` | Object | <optional> | The configuration object

  Source:
    - ClearBlade.js, line 1896

  ##### 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");});