If you're planning to stomp all over some sockets this little angularity to deal with STOMP queues is just the thing for you. On the big iron, server side, we're using Spring.
- If you're using bower just install:
bower install -save stompie
- OK I know it's obvious but add
<script>
tags to the module and the two required dependencies:
<script src="/bower_components/sockjs/sockjs.min.js"></script>
<script src="/bower_components/stomp-websocket/lib/stomp.min.js"></script>
<script src="/bower_components/stompie/stompie-min.js"></script>
- Declare the module as a dependency in your application:
angular.module('yourApplication', ['stompie']);
- Inject it in your controller:
angular.module('yourApplication')
.controller('YourCtrl', ['$stompie', '$scope', function ($stompie, $scope) {
// ...
}
- Use and subscribe:
$stompie.using('/your/stomp/endpoint', function () {
// The $scope bindings are updated for you so no need to $scope.$apply.
// The subscription object is returned by the method.
var subscription = $stompie.subscribe('/your/topic', function (data) {
$scope.foo = data;
});
// Unsubscribe using said subscription object.
subscription.unsubscribe();
// Send messages to a STOMP broker.
$stompie.send('/some/queue', {message: 'some message'});
// Disconnect from the socket.
$stompie.disconnect(function () {
// Called once you're out...
});
});
- You can call
$stompie.using()
as many times as you want, it will reuse the same socket connection. - The underlying libraries also mean that you can register multiple subscribers.
- The
$stompie.subscribe()
callback will attempt to parse the response. - Objects you pass to
$stompie.send()
will be stringified.
Bedtime reading:
For everything else, there's MasterCard.