# fruster-web-bus-client

> client for using the fruster web bus

Latest version **0.0.8** (published 2019-03-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install fruster-web-bus-client
pnpm add fruster-web-bus-client
yarn add fruster-web-bus-client
bun add fruster-web-bus-client
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2019-03-07 |
| First published | 2017-02-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 28.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | florry, jenkins-frost |

## Links

- npm: https://www.npmjs.com/package/fruster-web-bus-client
- Repository: https://github.com/FrostDigital/fruster-web-bus-client
- Homepage: https://github.com/FrostDigital/fruster-web-bus-client#readme
- Issues: https://github.com/FrostDigital/fruster-web-bus-client/issues
- npm.io page: https://npm.io/package/fruster-web-bus-client

## Recent versions

- 0.0.8 (latest) — 2019-03-07
- 0.0.7 — 2017-12-08
- 0.0.6 — 2017-12-01
- 0.0.5 — 2017-12-01
- 0.0.4 — 2017-12-01
- 0.0.3 — 2017-02-15
- 0.0.2 — 2017-02-15
- 0.0.1 — 2017-02-15
- 1.0.0 — 2017-02-15

## README

# Fruster web bus client

The fruster bus client looks at the subject of incoming messages from the server and redirects them to the corresponding handlers. All handlers for a specific subject will be triggered by an incoming message. 
By default, the subscribe function registers a handler on the specific subject with an id of that subject to prevent stock up in a reactive application. If a specific subject needs several things to happen in different places in the application, unique ids can be given to make sure they don´t overwrite each other. 

Subjects are defined by the backend services in similar fashion to `ws.out.:userId.*`.

*Example subject*: `ws.out.d5d20597.video-session.start`

**Instantiate the bus:**

    var bus = new FrusterWebBusClient("ws://localhost:3000");
    
**Register a handler for a specific subject. Every new subscribe will overwrite any previous handler (without id) for that subject.**

    bus.subscribe("ws.out.d5d20597.new-message", (msg) => {
        console.log("Subscribed to subject ws.out.d5d20597.new-message");
    });

**Register a handler for a specific subject without overwriting previous handlers using ids (id = **VIDEO\_SESSION\_HANDLER\_ID**):**
    
    bus.subscribe("ws.out.d5d20597.video-session.start", "VIDEO_SESSION_HANDLER_ID", (msg) => {
        console.log("Subscribed to subject ws.out.d5d20597.video-session.start with id VIDEO_SESSION_HANDLER_ID");
    });

**Register a handler for a specific subject overwriting previous handler using a specific id (id = **VIDEO\_SESSION\_HANDLER\_ID**):**
    
    bus.subscribe("ws.out.d5d20597.video-session.start", "VIDEO_SESSION_HANDLER_ID", (msg) => {
        console.log("overrides the previous handler for subject ws.out.d5d20597.video-session.start with id VIDEO_SESSION_HANDLER_ID");
    });

**Remove a handler for a specific subject and id (id = **VIDEO\_SESSION\_HANDLER\_ID**):**
    
    bus.unsubscribe("ws.out.d5d20597.video-session.start", "VIDEO_SESSION_HANDLER_ID");

**Remove all handlers for a specific subject:**

    bus.unsubscribe("ws.out.d5d20597.video-session.start");
    
**Register bus error handler:**

    bus.onError(() => {
        console.error("ERROR");
    });

**Register bus open handler:**

    bus.onOpen(() => {
        console.error("OPENED");
    });

**Register bus close handler:**

    bus.onClose(() => {
        console.error("CLOSED");
    });


## Requests
Requests can be made to specific fruster websocket endpoints. The endpoints uses the same methods as http, e.g. POST, GET etc. Example endpoint `ws.get.user.:userId` which can be called as `ws.get.user.4b2a50c5-f8fc-465c-a8e1-bfa978767396` to get the user with the id `4b2a50c5-f8fc-465c-a8e1-bfa978767396`.

    const response = await bus.request({
        subject: `ws.post.chat`, // endpoint 
        message: { // request body
            data: {
                message: $("#message").val()
            }
        }
    });

Response is returned in the same fashion as http endpoints in fruster.

For more examples, see [FrostDigital/fruster-web-bus-client examples](https://github.com/FrostDigital/fruster-web-bus-client/tree/master/examples)


## Development

To start webpack in watch mode:

    npm start

To build project using webpack and generate typings:

    npm run build

---
_Source: https://npm.io/package/fruster-web-bus-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
