12.7.5 • Published 2 days ago

happner-client v12.7.5

Weekly downloads
231
License
MIT
Repository
github
Last release
2 days ago

npmBuild StatusCoverage Status

happner-client

The client for happner-2 and happner cluster services.

npm install happner-client

Usage

Create client instance.

var HappnerClient = require('happner-client');
var client = new HappnerClient({
  requestTimeout: 10 * 1000, // (default) milliseconds timeout on api request (set ack)
  responseTimeout: 20 * 1000, // (default) timeout awaiting response
  logger: null // (defualt) optional happner-logger
});

Connect

var optionalInfo = {
  // meta data for login
  ///////////////////// in $origin
}
client.connect(
  { // connection
    host: 'localhost',
    port: 55000
  },
  { // options
    protocol: 'https',
  	username: '_ADMIN',
    password: 'happn',
    allowSelfSignedCerts: true,
    info: {}
  }
).then(...).catch(...); // also supports callback


// connection can be defaulted (eg. in browser)
client.connect(null, {username: '_ADMIN', password: 'happn'}, function (e) {
})

Note: The connect() function accepts the same arguments as the Happn.client.create() function and can therefore accept a list or range of redundant connections.

client.connect([
  {host: '10.0.0.1'},
  {host: '10.0.0.3'},
  {host: '10.0.0.8'}
], {
  username: '_ADMIN',
  password: 'happn',
}).then(...

Events

client.on('connected', function () {
  // event fired on successful connection to server
});

client.on('reconnected', function () {
  // event fired on successful reconnection to server
});

client.on('disconnected', function () {
  // event fired when disconnected from server
});

client.on('reconnecting', function () {
  // event fired when attempting to reconnect
});

client.on('error', function (e) {
  // includes model verification mismatches
});

Construct your API

var kitchenModel = {
  fridge: {
    version: '^1.0.0', // requires that server has matching version of fridge component
    methods: {
      getTemperature: {
        // optional parameters for clientside validation
        params: [
          {name: 'shelves', type: 'array'}
        ]
      } 
    }
  }
};

var kitchen = client.construct(kitchenModel);

Use API functions

// with callback
kitchen.exchange.fridge.getTemperature(['top', 'middle'], function (e, temps) {});

// with promise
kitchen.exchange.fridge.getTemperature(['top', 'middle'])
	.then(function (temps) {})
	.catch(function (e) {})

Listen to API events

kitchen.event.fridge.on('/eventName', function (data) {});

Browser usage

Assuming served from happner-2 packaged /api/client script

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  
  <!-- includes Happner.HappnerClient -->
  <script src="/api/client"></script>

  </head>
<body>

  <script>

    var client = new Happner.HappnerClient({
      requestTimeout: 10 * 1000,
      responseTimeout: 20 * 1000
    });

    var model = {
      'component': {
        version: '^2.0.0',
        methods: {
          method1: {}
        }
      }
    };

    var api = client.construct(model);

    client.connect()

      .then(function () {
        // subscribe to events (requires connected)
        api.event.component.on('test/event', function (data, meta) {
          console.log('EVENT', meta.path);
        });
      })

      .catch(function (error) {
        console.error('connection error', error);
      });
    
    // repeat call on exchange
    setInterval(function () {

      api.exchange.component.method1()
        .then(function (reply) {
          console.log('REPLY', reply);
        })
        .catch(function (error) {
          console.error('ERROR', error);
        });

    }, 1000);

  </script>

</body>
</html>
12.7.5

1 month ago

12.7.2-prerelease-1

10 months ago

12.7.1

12 months ago

12.7.2

10 months ago

12.7.3

10 months ago

12.7.4

9 months ago

12.6.1

1 year ago

12.7.0

1 year ago

12.6.0

1 year ago

12.5.8

1 year ago

12.5.6

1 year ago

12.5.7

1 year ago

12.5.2

2 years ago

12.5.3

1 year ago

12.5.4

1 year ago

12.5.5

1 year ago

12.5.0

2 years ago

12.5.1

2 years ago

12.4.0

2 years ago

12.4.1

2 years ago

12.3.0

2 years ago

12.3.1

2 years ago

12.2.5

2 years ago

12.2.6

2 years ago

12.2.7

2 years ago

12.2.8

2 years ago

12.2.4

2 years ago

12.2.1

2 years ago

12.2.2

2 years ago

12.2.3

2 years ago

12.0.3

2 years ago

12.0.4

2 years ago

12.0.5

2 years ago

12.0.2

2 years ago

12.2.0

2 years ago

12.1.0

2 years ago

12.0.0

2 years ago

12.0.1

2 years ago

11.6.6

2 years ago

11.6.7

2 years ago

11.6.0

2 years ago

11.6.1

2 years ago

11.6.4

2 years ago

11.6.5

2 years ago

11.6.2

2 years ago

11.6.3

2 years ago

11.5.5

2 years ago

11.5.6

2 years ago

11.5.4

3 years ago

11.5.3

3 years ago

11.5.2

3 years ago

11.5.1

3 years ago

11.5.0

3 years ago

11.4.0

3 years ago

11.3.1

3 years ago

11.3.0

3 years ago

11.2.1

3 years ago

11.2.0

3 years ago

11.1.9

3 years ago

11.1.8

3 years ago

11.1.7

3 years ago

11.1.6

4 years ago

11.1.5

4 years ago

11.1.4

4 years ago

11.1.3

4 years ago

11.1.2

4 years ago

11.1.1

4 years ago

11.1.0

4 years ago

11.0.4

4 years ago

11.0.3

4 years ago

11.0.2

4 years ago

11.0.1

4 years ago

11.0.1-rc-1

4 years ago

11.0.0

4 years ago

10.0.0

4 years ago

9.0.0

5 years ago

8.1.2

5 years ago

8.1.1

5 years ago

8.1.0

5 years ago

8.0.1

5 years ago

8.0.0

5 years ago

7.0.1

5 years ago

7.0.0

5 years ago

6.3.0

5 years ago

6.2.0

5 years ago

6.1.0

6 years ago

6.0.3

6 years ago

6.0.2

6 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.1.0

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.1-debug.2

6 years ago

5.0.1-debug.1

6 years ago

5.0.1-debug.0

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.0

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago