# angular-message-socket

> A WebSocket wrapper for Angular with Akka-style semantics.

Latest version **0.1.0** (published 2014-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install angular-message-socket
pnpm add angular-message-socket
yarn add angular-message-socket
bun add angular-message-socket
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2014-09-20 |
| First published | 2014-09-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dan Baumann |
| Maintainers | dbaumann |
| Keywords | angular, angularjs, websocket, akka, mocks, testing, fluent interface |

## Links

- npm: https://www.npmjs.com/package/angular-message-socket
- Repository: https://github.com/dbaumann/angular-message-socket
- Issues: https://github.com/dbaumann/angular-message-socket/issues
- npm.io page: https://npm.io/package/angular-message-socket

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2014-09-20

## README

## angular-message-socket
A WebSocket wrapper for Angular with Akka-style semantics.


## Usage

### App
```javascript
var myAppModule = angular.module("MyApp", ["dbaumann.message-socket"]);

myAppModule.factory("myService", function(MessageSocket) {
  var messenger = new MessageSocket("ws://localhost:3000");

  return {
    doTell: function() {
      // fire and forget
      messenger.tell({_: "ProfileUpdate", profile: { name: "Kevin Flynn" }});

      // listen
      messenger.receive("ProfileUpdateDone", function(event) { /*...*/ });
    },

    doAsk: function() {
      // ask (with $q)
      messenger.ask = messenger.withTimeout(5000);
      var profileAsk = messenger.ask({_: "ProfileUpdate", profile: { name: "Kevin Flynn" }});

      Promise.race([
        profileAsk.for("ProfileUpdateDone").then(function(event) { /*...*/ }),
        profileAsk.for("ActionFailed").then(function(event) { /*...*/ })
      ]).catch(function(error) {
        console.error(error);
      });
    }
  }
});
```

### Configuration
```javascript
myAppModule.config(function($logProvider, MessageSocketProvider) {
  // enable console messages
  $logProvider.debugEnabled(true);
  
  // message type key (defaults to "$variant")
  MessageSocketProvider.messageTypeKey("verb");

  // messages sent while connection is down are placed in a bounded queue
  MessageSocketProvider.delayedQueueSize(200);

  // when notified of a lost connection, will attempt to reconnect per RFC 6455
  MessageSocketProvider.reconnectTimeout(1000);
  MessageSocketProvider.maxReconnects(10);
});
```

### Testing
`WebSocketBackend` is available for your mocking needs, inspired by `$httpBackend`.
Just make _dist/angular-message-socket-mocks.js_ available to your tests.

```javascript
describe("MyApp", function() {
  beforeEach(module("MyApp", "mock.WebSocket"));

  describe("myService", function() {
    var $timeout, WebSocketBackend, createMessageSocket, mySocket;
    beforeEach(inject(function(_$timeout_, _WebSocketBackend_, _MessageSocket_) {
      $timeout = _$timeout_;
      WebSocketBackend = _WebSocketBackend_;
      createMessageSocket = function() {
        var ret = new _MessageSocket_("ws://localhost:3000");
        $timeout.flush(); // necessary for mock to initialize
        return ret;
      };
    }));

    it("should handle request/response pattern", function() {
      mySocket = createMessageSocket();

      WebSocketBackend.when(function(m) { return m.$variant == "ProfileUpdate" },
        // stamp attribute used by ask to identify responses
        {"$variant": "ProfileUpdateDone",
         profile: { name: "Kevin Flynn" },
         "stamp":Date.now()}
      );

      // ... code using MessageSocket

      WebSocketBackend.flush();

      // ... expectations

      expect(WebSocketBackend.outstandingRequests().length).toBe(0);
    });

    it("should handle push", function() {
      mySocket = createMessageSocket();

      WebSocketBackend.push(
        {"$variant": "ProfileUpdateDone",
         profile: { name: "Kevin Flynn" },
         "stamp":Date.now()}
      );

      // ... expectations
    });
  });
});
```

More examples of `MessageSocket` and `WebSocketBackend` can be found in test/spec/tests.js.


## Development

Code quality is ensured with CoffeeScript, CoffeeLint, and Karma.
```sh
npm install -g grunt-cli
npm install && bower install
grunt
```

### Live Reloading

```sh
grunt karma:unit:start watch
```

### Build

```sh
grunt dist
```

---
_Source: https://npm.io/package/angular-message-socket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
