Class: Client

Defined in: src/stomp.coffee

Overview

STOMP Client Class

All STOMP protocol is exposed as methods of this class (connect(), send(), etc.)

Instance Method Summary

Constructor Details

# (void) constructor(ws_fn) Private

Please do not create instance of this class directly, use one of the methods Stomp~client, Stomp~over or {overTCP} in Stomp.

See also:

Instance Method Details

# (void) debug(message)

Note: the default can generate lot of log on the console. Set it to empty function to disable

By default, debug messages are logged in the window's console if it is defined. This method is called for every actual transmission of the STOMP frames over the WebSocket.

It is possible to set a debug(message) method on a client instance to handle differently the debug messages:

Examples:

  client.debug = function(str) {
      // append the debug log to a #debug div
      $("#debug").append(str + "\n");
  };

disable logging

  client.debug = function(str) {};

Parameters:

  • message ( String )

~ (void) connect(headers, connectCallback)
~ (void) connect(headers, connectCallback, errorCallback)
~ (void) connect(login, passcode, connectCallback)
~ (void) connect(login, passcode, connectCallback, errorCallback)
~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback)
~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback, host)

Note: When auto reconnect is active, connectCallback and errorCallback will be called on each connect or error

The connect method accepts different number of arguments and types. See the Overloads list. Use the version with headers to pass your broker specific options.

Examples:

     client.connect('guest, 'guest', function(frame) {
       client.debug("connected to Stomp");
       client.subscribe(destination, function(message) {
         $("#messages").append("<p>" + message.body + "</p>\n");
       });
     });

Parameters:

  • headers ( Object )
  • connectCallback ( function(Frame) ) Called upon a successful connect or reconnect
  • errorCallback ( function(any) ) Optional, called upon an error. The passed paramer may be a Frame or a message
  • closeEventCallback ( function(CloseEvent) ) Optional, called when the websocket is closed.
  • login ( String )
  • passcode ( String )
  • host ( String ) Optional, virtual host to connect to. STOMP 1.2 makes it mandatory, however the broker may not mandate it

Options Hash: (headers):

  • login ( String )
  • passcode ( String )
  • host ( String ) virtual host to connect to. STOMP 1.2 makes it mandatory, however the broker may not mandate it

See also:

Overloads:

~ (void) connect(headers, connectCallback)

~ (void) connect(headers, connectCallback, errorCallback)

~ (void) connect(login, passcode, connectCallback)

~ (void) connect(login, passcode, connectCallback, errorCallback)

~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback)

~ (void) connect(login, passcode, connectCallback, errorCallback, closeEventCallback, host)

# (void) disconnect(disconnectCallback, headers = {})

Disconnect from the STOMP broker. To ensure graceful shutdown it sends a DISCONNECT Frame and wait till the broker acknowledges.

disconnectCallback will be called only if the broker was actually connected.

Parameters:

  • disconnectCallback ( function() )
  • headers ( Object ) optional

See also:

# (void) send(destination, headers = {}, body = '')

Note: Body must be String. You will need to covert the payload to string in case it is not string (e.g. JSON)

Send a message to a named destination. Refer to your STOMP broker documentation for types and naming of destinations. The headers will, typically, be available to the subscriber. However, there may be special purpose headers corresponding to your STOMP broker.

Examples:

  client.send("/queue/test", {priority: 9}, "Hello, STOMP");

payload without headers

  # If you want to send a message with a body, you must also pass the headers argument.
  client.send("/queue/test", {}, "Hello, STOMP");

Parameters:

  • destination ( String ) mandatory
  • headers ( Object ) Optional
  • body ( String ) Optional

See also:

# (Object) subscribe(destination, callback, headers = {})

Note: The library will generate an unique ID if there is none provided in the headers. To use your own ID, pass it using the headers argument

Subscribe to a STOMP Broker location. The return value is an Object with unsubscribe method.

var subscription = client.subscribe("/queue/test", callback);

Examples:

 callback = function(message) {
   // called when the client receives a STOMP message from the server
   if (message.body) {
     alert("got message with body " + message.body)
   } else
   {
     alert("got empty message");
   }
 });

Explicit subscription id

   var mysubid = 'my-subscription-id-001';
   var subscription = client.subscribe(destination, callback, { id: mysubid });

Parameters:

  • destination ( String )
  • callback ( function(message) )
  • headers ( Object ) optional

Returns:

  • ( Object ) — this object has a method to unsubscribe

See also:

# (void) unsubscribe(id, headers = {})

It is preferable to unsubscribe from a subscription by calling unsubscribe() directly on the object returned by client.subscribe():

Examples:

  var subscription = client.subscribe(destination, onmessage);
  ...
  subscription.unsubscribe();

Parameters:

  • id ( String )
  • headers ( Object ) optional

See also:

# (Object) begin(transaction_id)

Note: If no transaction ID is passed, one will be created automatically

Start a transaction, the returned Object has methods - commit and abort

Parameters:

  • transaction_id ( String ) optional

Returns:

  • ( Object ) — member, id - transaction id, methods commit and abort

See also:

# (void) commit(transaction_id)

Commit a transaction. It is preferable to commit a transaction by calling commit() directly on the object returned by client.begin():

Examples:

  var tx = client.begin(txid);
  ...
  tx.commit();

Parameters:

  • transaction_id ( String )

See also:

# (void) abort(transaction_id)

Abort a transaction. It is preferable to abort a transaction by calling abort() directly on the object returned by client.begin():

Examples:

  var tx = client.begin(txid);
  ...
  tx.abort();

Parameters:

  • transaction_id ( String )

See also:

# (void) ack(messageID, subscription, headers = {})

ACK a message. It is preferable to acknowledge a message by calling ack() directly on the message handled by a subscription callback:

Examples:

  client.subscribe(destination,
    function(message) {
      // process the message
      // acknowledge it
      message.ack();
    },
    {'ack': 'client'}
  );

Parameters:

  • messageID ( String )
  • subscription ( String )
  • headers ( Object ) optional

See also:

# (void) nack(messageID, subscription, headers = {})

NACK a message. It is preferable to nack a message by calling nack() directly on the message handled by a subscription callback:

Examples:

  client.subscribe(destination,
    function(message) {
      // process the message
      // an error occurs, nack it
      message.nack();
    },
    {'ack': 'client'}
  );

Parameters:

  • messageID ( String )
  • subscription ( String )
  • headers ( Object ) optional

See also:

    Quickly fuzzy find classes, mixins, methods, file:

    Control the navigation frame:

    You can focus and blur the search input: