An Angular WebSocket service for connecting client applications to servers.
npm install angular-websocket





An AngularJS 1.x WebSocket service for connecting client applications to servers.
You can download angular-websocket by:
* (prefered) Using bower and running bower install angular-websocket --save
* Using npm and running npm install angular-websocket --save
* Downloading it manually by clicking here to download development unminified version
* CDN for development https://rawgit.com/gdi2290/angular-websocket/v1.0.9/angular-websocket.js
* CDN for production https://cdn.rawgit.com/gdi2290/angular-websocket/v1.0.9/angular-websocket.min.js
``html`
returns instance of $Websocket
name | arguments | description
------------|--------------------------------------------------------|------------
$websocket
_constructor_ | url:String | Creates and opens a WebSocket instance.
var ws = $websocket('ws://foo');data
send | data:String,Object returns | Adds data to a queue, and attempts to send if socket is ready. Accepts string or object, and will stringify objects before sending to socket.
onMessage | callback:Function
options{filter:String,RegExp, autoApply:Boolean=true} | Register a callback to be fired on every message received from the websocket, or optionally just when the message's property matches the filter provided in the options object. Each message handled will safely call $rootScope.$digest() unless autoApply is set to false in the options. Callback gets called with a MessageEvent object.
onOpen | callback:Function | Function to be executed each time a socket connection is opened for this instance.
onClose | callback:Function | Function to be executed each time a socket connection is closed for this instance.
onError | callback:Function | Function to be executed each time a socket connection has an Error for this instance.
close | force:Boolean:_optional_ | Close the underlying socket, as long as no data is still being sent from the client. Optionally force close, even if data is still being sent, by passing true as the force parameter. To check if data is being sent, read the value of socket.bufferedAmount.
send calls to be made on socket when socket is able to receive data. List is populated by calls to the send method, but this array can be spliced if data needs to be manually removed before it's been sent to a socket. Data is removed from the array after it's been sent to the socket.onOpen method.onMessage method.onError method.onClose method.This type is returned from the send() instance method of $websocket, inherits from $q.defer().promise.
name | arguments | description
------------|--------------------------------------------------------|------------
cancel | | Alias to deferred.reject(), allows preventing an unsent message from being sent to socket for any arbitrary reason.
then | resolve:Function, reject:Function | Resolves when message has been passed to socket, presuming the socket has a readyState of 1. Rejects if the socket is hopelessly disconnected now or in the future (i.e. the library is no longer attempting to reconnect). All messages are immediately rejected when the library has determined that re-establishing a connection is unlikely.
Similar to httpBackend mock in
AngularJS's ngMock module. You can use ngWebSocketMock to mock a websocket
server in order to test your applications:
``javascript
var $websocketBackend;
beforeEach(angular.mock.module('ngWebSocket', 'ngWebSocketMock');
beforeEach(inject(function (_$websocketBackend_) {
$websocketBackend = _$websocketBackend_;
$websocketBackend.mock();
$websocketBackend.expectConnect('ws://localhost:8080/api');
$websocketBackend.expectSend({data: JSON.stringify({test: true})});
}));
`
name | arguments | description
-------------------------------|------------|-----------------------------------
flush | | Executes all pending requests
expectConnect | url:String | Specify the url of an expected WebSocket connection
expectClose | | Expect "close" to be called on the WebSocket
expectSend | msg:String | Expectation of send to be called, with required message
verifyNoOutstandingExpectation | | Makes sure all expectations have been satisfied, should be called in afterEach
verifyNoOutstandingRequest | | Makes sure no requests are pending, should be called in afterEach
Q.*: What if the browser doesn't support WebSockets?
A.*: This module will not help; it does not have a fallback story for browsers that do not support WebSockets. Please check your browser target support here and to include fallback support.
`shell`
$ npm install
$ bower install
Run karma in Chrome, Firefox, and Safari$3
In the project root directory open
index.html in the example folder or browserify example$3
$ npm run dist` Builds files with uglifyjs* Slack: AngularClass
* Gitter: angularclass/angular-websocket
* Twitter: @AngularClass
enjoy — AngularClass

##AngularClass
> Learn AngularJS, Angular 2, and Modern Web Development form the best.
> Looking for corporate Angular training, want to host us, or Angular consulting? patrick@angularclass.com