SockJS provider for AngularJS.
Based on Brian Ford's angular-socket-io.
bower install angular-sockjs
- Made sure the SockJS client library is loaded.
- Include the
socket.js
script provided by this component into your app. - Add
bd.sockjs
as a module dependency to your app.
This module exposes a socketFactory
, which is an API for instantiating
sockets that are integrated with Angular's digest cycle.
// in the top-level module of the app
angular.module('myApp', [
'bd.sockjs',
'myApp.MyCtrl'
])
.factory('mySocket', function (socketFactory) {
return socketFactory();
});
With that, you can inject your mySocket
service into controllers and
other serivices within your application!
Building on the example above:
// in the top-level module of the app
angular.module('myApp', [
'bd.sockjs',
'myApp.MyCtrl'
])
.factory('mySocket', function (socketFactory) {
return socketFactory({
url: 'https://example.com'
});
})
.controller('MyController', function (mySocket) {
// ...
});
Since the SockJS API uses single event handlers rather than Socket.io's event emitter style, this library provides a lightweight wrapper around the handlers.
Accepts valid socket events: open
, close
, and message
. Returns socket wrapper so multiple calls can be chained.
Handles the same events as socket.setHandler
. Returns socket wrapper so multiple calls can be chained.
Sends a socket message.
Attempts to close the socket.
This option allows you to provide the socket
service with a SockJS
instance to be used internally.
This is useful if you need to hold a reference to the sockjs
object for use elsewhere.
angular.module('myApp', [
'bd.sockjs'
])
.factory('mySocket', function (socketFactory) {
var sockjs = new SockJS('http://host');
mySocket = socketFactory({
socket: sockjs
});
return mySocket;
});
This option allows you to provide a URL to pass through to the SockJS constructor.
MIT