stomp-js - STOMP in node.js
Intro
OK, so I suck at this. I set out to write at least two, maybe three blog posts last night. One of the other two is going to require much more thought and editing, the third is about vaporware (I literally have one test done) and this one, well, this one is the easiest because most of the work is already done ;).
Pretty simple what's going on here, but lets step through it anyway.
- Setup arguments to pass to the constructor
- Create the object
- Setup some options for subscribing to the destination
- Connect to the broker
- Subscribe to events
- When 'connected' is fired, subscribe to the desired destination
- When 'message' is fired, ack the message (you could do fancy things with the message here)
- When 'error' is fired, inform of the error and disconnect
- When 'SIGINT' is caught by the process, report a tally of work done and disconnect
This will listen for messages until we kill it, or if an error is encountered. It's single purpose is to consume all the messages and acknowledge them with the broker.
Now we can't just let that consumer sit there being all lazy, lets send it some messages.
A stomp-js producer:
Stepping through this briefly.
- Get number of messages to produce from the arguments
- Decided if we want to get a receipt for messages produced
- Setup arguments for the constructor
- Create the object
- Set the destination for messages
- Connect to the broker
- Subscribe to events
- When 'connected' is fired, loop 'n' times, sending a message to the broker on each iteration. Disconnect when complete.
- When 'receipt' is fired, report the receipt ID
- When 'error' is fired, inform of the error and disconnect
- When 'SIGINT' is caught by the process, report a tally of work done and disconnect
Pretty simple stuff!
I'd like to point out that stomp-js does implement the basic transaction support defined by STOMP 1.0. Below is some example code showing how to implement this.
Basically you call client.begin(), which returns a transaction id. Then add a 'transaction' header to the message you're sending with the transaction id as the value. Once the send is done, call client.commit(transaction_id).
Here is the full example:
Fin
That's basically it!
Want to help make stomp-js better? Have an issue with it? Want to keep up with progress? Follow/Fork/Report on the github page! https://github.com/benjaminws/stomp-js
