swindon

0.5.1 • Public • Published

SwindonJS

Status: Beta

Documentation

Makes it easier to chain ur async actions with ws and structured data in send method, controls connect state.

Installation

    npm install --save -E swindon@0.5.1

Basic Usage

Initializing Swindon:

    import { Swindon } from 'swindon';
 
    const swindon = new Swindon('/ws');

This creates a auto-reconnecting instance of the library. It's adviced that this object a singleton as swindon allows multiple applications to be joined together using single websocket.

Now we can make method calls:

    const result = await swindon.call("my_method", ['arg1'], {'kwarg1': 1})

Technically you can wait for connection to be established:

    await swindon.waitConnected()

But that is rarely useful, because we usually queue call's internally.

Getting User Profile

Swindon sends minimal user info (at least user_id) right after connection is established (technically in hello message). You can fetch it like this:

    const user_info = await swindon.waitConnected()

Subscriptions

Because swindon allows only subcriptions and unsubscriptions from the backend we combine "subscription" API with the method call:

    const guard = swindon.guard()
        .init('notifications.subscribe', [mytopic])
        .listen('notifications.'+mytopic, message => {
            // react-like code
            this.setState(message.n_notifications)
        })
        .deinit('notifications.unsubscribe', [mytopic])

Then when component doesn't need the data any more just call "close":

    guard.close()

Note: notifications.subscribe(mytopic) will be called on every reconnect of the websocket, until close is called.

Readme

Keywords

Package Sidebar

Install

npm i swindon

Weekly Downloads

818

Version

0.5.1

License

MIT

Last publish

Collaborators

  • hunson.abadeer
  • tailhook