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
- # (void) debug(message) By default, debug messages are logged in the window's console if it is defined.
-
~
(void)
connect(headers, connectCallback)
The
connect
method accepts different number of arguments and types. - # (void) disconnect(disconnectCallback, headers = {}) Disconnect from the STOMP broker.
- # (void) send(destination, headers = {}, body = '') Send a message to a named destination.
- # (Object) subscribe(destination, callback, headers = {}) Subscribe to a STOMP Broker location.
-
#
(void)
unsubscribe(id, headers = {})
It is preferable to unsubscribe from a subscription by calling
unsubscribe()
directly on the object returned byclient.subscribe()
: -
#
(Object)
begin(transaction_id)
Start a transaction, the returned Object has methods -
commit
andabort
- # (void) commit(transaction_id) Commit a transaction.
- # (void) abort(transaction_id) Abort a transaction.
- # (void) ack(messageID, subscription, headers = {}) ACK a message.
- # (void) nack(messageID, subscription, headers = {}) NACK a message.
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.
Instance Method Details
#
(void)
debug(message)
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) {};
~
(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)
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");
});
});
#
(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.
#
(void)
send(destination, headers = {}, body = '')
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");
#
(Object)
subscribe(destination, callback, headers = {})
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 });
#
(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();
#
(Object)
begin(transaction_id)
Start a transaction, the returned Object has methods - commit
and abort
#
(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();
#
(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();
#
(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'}
);
#
(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'}
);