# ks-client

> nodejs client for Kaltiot Smart IoT SDK

Latest version **3.1.4** (published 2024-08-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install ks-client
pnpm add ks-client
yarn add ks-client
bun add ks-client
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.4 |
| Published | 2024-08-20 |
| First published | 2016-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 59.2 KB |
| Known vulnerabilities | 0 (+11 in 1 direct dependencies) |
| Install scripts | yes |
| Author | Kaltio Technologies |
| Maintainers | kaltiot-admin |

## Links

- npm: https://www.npmjs.com/package/ks-client
- Repository: https://bitbucket.org/kaltiot/ks-client-node
- Homepage: https://bitbucket.org/kaltiot/ks-client-node#readme
- Issues: https://bitbucket.org/kaltiot/ks-client-node/issues
- npm.io page: https://npm.io/package/ks-client

## Dependencies (9)

- [lodash](https://npm.io/package/lodash.md) ^4.12.0
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [bluebird](https://npm.io/package/bluebird.md) ^3.3.5
- [protobufjs](https://npm.io/package/protobufjs.md) ^5.0.1
- [mkdirp-then](https://npm.io/package/mkdirp-then.md) ^1.2.0
- [@kaltiot/ks-ipc](https://npm.io/package/@kaltiot/ks-ipc.md) ^2.1.2
- [readable-stream](https://npm.io/package/readable-stream.md) ^1.0.33
- [@kaltiot/ks-constants](https://npm.io/package/@kaltiot/ks-constants.md) ^1.1.0
- [@kaltiot/eslint-config](https://npm.io/package/@kaltiot/eslint-config.md) ^1.0.3

## Recent versions

- 3.1.4 (latest) — 2024-08-20
- 3.1.3 — 2024-08-20
- 3.1.2 — 2018-01-10
- 3.1.0 — 2018-01-10
- 3.0.0 — 2016-12-21
- 2.0.0 — 2016-08-11
- 1.0.3 — 2016-06-21
- 1.0.2 — 2016-06-06
- 1.0.1 — 2016-06-06
- 1.0.0 — 2016-06-03

## README

# ks-client
## API

All the functions that accept a callback also return promises. So you can use the library in the usual callback style or promise style, whichever you prefer.

**Requires C gateway version >= 1.0.18 or JS gateway version >= 2.0.0.**

**Example**

```js
var KSClient = require("ks-client");
var constants = KSClient.constants;
var client = new KSClient;

var path = "/tmp/ks_gw_socket";

// this is equivalent to...
client.connect(path, function(err) {
  if (err) {
    // handle error
    return;
  }

  // handle success
});

// ...this
client.connect(path).then(function () {
  // handle success
}).catch(function (err) {
  // handle error
});
```

### new KSClient

Creates a new client object.

The client is an EventEmitter and it emits the following events.

#### Event: ipc-disconnected

Fired when the IPC gets disconnected. You will need to call `#connect` again after this.

#### Event: state

Fired when the state of the gateway changes.

Field | Type | Description
------|------|------------
state | int | Represents the state of the client. One of the **network state** constants from [constants.md](constants.md).
error | int | Possible error code. One of the **error** constants from [constants.md](constants.md). 

#### Event: notification

Fired when a notification is received.

Field | Type | Description
------|------|------------
address | string | Alphanumeric string.
payloadType | string | Type of payload. One of: "BINARY", "INT", "STRING", "PING", "PONG".
payload | int, Buffer or string | 32 bit integer when payloadType is "INT", Buffer when "BINARY", string otherwise.

#### Event: rid

Fired when your application receives its RID.

Field | Type | Description
------|------|------------
address | string | Alphanumeric string.
rid | string | The RID received.
secret | string | The secret of that RID.

#### Event: gateway-rid

Fired when the gateway receives its RIDs.

Field | Type | Description
------|------|------------
rid | string | The gateway RID.

### #connect(path, [callback])

Connect to a socket in the given address or path. For example, `/tmp/ks_gw_socket` or `localhost:12345`.

### #disconnect([callback])

Disconnects an established connection, if any.

### #register(address, version, customerId, channels, [callback])

Registers with the daemon. App will start receiving notifications and other events.

NOTE: If you need to update the channels, you have to first unregister nd then register again, with the different channels.

Argument | Type | Description
---------|------|------------
address | string | Alphanumeric string.
version | string | Alphanumeric string.
customerId | string | Your customer ID. Alphanumeric.
channels | Array | Array of strings describing the channels you wish to subscribe to.

### #unregister(address, version, customerId, [callback])

Unregisters from the daemon.

Argument | Type | Description
---------|------|------------
address | string | Alphanumeric string.
version | string | Alphanumeric string.
customerId | string | Your customer ID. Alphanumeric.

### #publish(payloadType, payload, [tag], [callback])

Publishes a message to the server.

Argument | Type | Description
---------|------|------------
payloadType | string | Type of payload. One of: "BINARY", "INT", "STRING", "PING", "PONG".
payload | int, Buffer or string | 32 bit integer when payloadType is "INT", Buffer when "BINARY", string otherwise.
tag | string | SRIKY WHAT WAS THIS AGAIN?

### #requestState([callback])

Request the state from the daemon. This will cause the `state` event to be emitted. The callback is run when the request has been written.

### #requestRid([callback])

Request your rid from the daemon. This will cause the `rid` event to be emitted. The callback is run when the request has been written.

### #requestAppId([callback])

Request the application ID from the daemon. Callback will be called with the application ID.

### #setNetworkAvailable(opts, [callback])

Set the availability of the network. `opts` is an object of the following format: 

```js
{
  state: constants.NETWORK_STATE_MOBILE_2G, // one of the network state constants
  mcc: "555", // optional, use with mobile networks
  mnc: "66", // optional, use with mobile networks
}
```

`mcc` and `mnc` are used for optimization of the connection. If they change, you should update the state.

### #setEngineEnabled(enabled, [callback])

Enable or disable the gateway.

Argument | Type | Description
---------|------|------------
enabled | boolean | 

### #setOrganizationSecret(secret, [callback])

Set the organization secret. This will only have an effect if it is called on an empty database before any applications have been registered.

Argument | Type | Description
---------|------|------------
secret | string | 

## Example use

A complete example that works similarly to the C SDK's `sample_publish` can be found in [src/lib/example.js](src/lib/example.js).

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