Home

Awesome

ngSocket Build Status

Status: In-Development

An AngularJS 1.x service for connecting applications to servers with WebSocket support.

Usage

bower install ngSocket

.controller('SomeController', function (ngSocket) {
  //Open a WebSocket connection
  var ws = ngSocket('ws://foo/bar');

  //Can call before socket has opened
  ws.send({foo: 'bar'});
});

API

Factory: ngSocket (in module ngSocket)

returns instance of NGWebSocket

Methods

nameargumentsdescription
ngSocket <br>constructorurl:StringCreates and opens a WebSocket instance. var ws = ngSocket('ws://foo');
senddata:String,Object returnsAdds data to a queue, and attempts to send if socket is ready. Accepts string or object, and will stringify objects before sending to socket.
onMessagecallback:Function <br>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 data 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.
onOpencallback:FunctionFunction to be executed each time a socket connection is opened for this instance.
closeforce:Boolean:optionalClose 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.

Properties

nametypedescription
socketwindow.WebSocketWebSocket instance.
sendQueueArray<function>Queue of 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.
onOpenCallbacksArray<function>List of callbacks to be executed when the socket is opened, initially or on re-connection after broken connection. Callbacks should be added to this list through the onOpen method.
onMessageCallbacksArray<function>List of callbacks to be executed when a message is received from the socket. Callbacks should be added via the onMessage method.
readyStateNumber:readonlyReturns either the readyState value from the underlying WebSocket instance, or a proprietary value representing the internal state of the lib, e.g. if the lib is in a state of re-connecting.

CancelablePromise

This type is returned from the send() instance method of ngSocket, inherits from $q.defer().promise.

Methods

nameargumentsdescription
cancelAlias to deferred.reject(), allows preventing an unsent message from being sent to socket for any arbitrary reason.
thenresolve:Function, reject:FunctionResolves 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.

Service: ngSocketBackend (in module ngSocketMock)

Similar to httpBackend mock in AngularJS's ngMock module

Methods

nameargumentsdescription
flushExecutes all pending requests
expectConnecturl:StringSpecify the url of an expected WebSocket connection
expectCloseExpect "close" to be called on the WebSocket
expectSendmsg:StringExpectation of send to be called, with required message
verifyNoOutstandingExpectationMakes sure all expectations have been satisfied, should be called in afterEach
verifyNoOutstandingRequestMakes sure no requests are pending, should be called in afterEach

Logical Questions

Development

$ npm install
$ bower install

Unit Tests

$ npm test Starts karma and watches files for changes

Manual Tests

In the project root directory:

$ node test-server Starts a sample web socket server to send/receive messages $ ./node_modules/.bin http-server - Basic http server to seve a static file Open localhost:8081/test-app.html and watch browser console and node console to see messages passing

Distribute

$ ./dist.sh For now just copies src/ngSocket.js to dist/ (bower is configured to ignore src/ and test, plus pretty much everything else)

TODO

License

Apache 2.0