# true-smart-messaging

> ``` npm install true-smart-messaging or yarn add true-smart-messaging ```

Latest version **1.3.0** (published 2019-09-10) · 0 weekly downloads

## Install

```sh
npm install true-smart-messaging
pnpm add true-smart-messaging
yarn add true-smart-messaging
bun add true-smart-messaging
```

## 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 | 1.3.0 |
| Published | 2019-09-10 |
| First published | 2018-11-12 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 105.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | True |
| Maintainers | ga-mo |

## Links

- npm: https://www.npmjs.com/package/true-smart-messaging
- npm.io page: https://npm.io/package/true-smart-messaging

## Dependencies (1)

- [socket.io-client](https://npm.io/package/socket.io-client.md) ^2.1.1

## Recent versions

- 1.3.0 (latest) — 2019-09-10
- 1.2.0-beta.5 (beta) — 2018-12-28
- 1.2.0 — 2018-12-28
- 1.2.0-beta.4 — 2018-12-28
- 1.2.0-beta.3 — 2018-12-28
- 1.2.0-beta.2 — 2018-12-21
- 1.2.0-beta.1 — 2018-12-19
- 1.1.0 — 2018-12-14
- 1.0.0 — 2018-12-07
- 1.0.0-beta.2 — 2018-12-07
- 1.0.0-beta.1 — 2018-12-07
- 0.2.1-beta.1 — 2018-12-07
- 0.2.0 — 2018-12-07
- 0.1.1-beta.0 — 2018-11-12
- 0.1.0-beta.0 — 2018-11-12

## README

## Installation

```
  npm install true-smart-messaging
  or
  yarn add true-smart-messaging
```

## Create Socket

```js
import trueSmartMessaging from 'true-smart-messaging';
const socket = trueSmartMessaging.connect('http://host.com');
```

## List of methods

- [Socket State](#socket-state)
- [Socket Disconnect](#socket-disconnect)
- [Connection State](#connection-state)
- [Create Room](#create-room)
- [Join Room](#join-room)
- [Leave Room](#leave-room)
- [Broadcast Room](#broadcast-room)
- [Events](#events)
- [Example Usage](#example-usage)
- [Socket State Helper](#socket-state-helper)

## Socket State

Listening to State from Socket server has change.

```js
socket.socketState(state => {
  // Do Something
});
```

## Socket Disconnect

Disconnects the socket manually.

```js
socket.disconnect();
```

## Connection State

```js
socket.connection.connect(() => console.log('Connected'));
socket.connection.disconnect(() => console.log('Disconnect'));
socket.connection.connectError(() => console.log('Connect Error'));
socket.connection.reconnecting(() => console.log('Reconnecting'));
```

## Create Room

For Setting page that read a config from the registry.

```js
const configs = {
  shop: {
    id: '001',
    name: 'Shop 1'
  },
  counter: {
    id: '1',
    name: 'Counter 1'
  }
};

socket.createRoom(configs);
```

## Join Room

Room's format is `{shop.id}-{counter.id}`

```js
socket.joinRoom('room', 'clientName', response => {
  // response will be { status, message }
});
```

## Leave Room

```js
socket.leaveRoom();
```

## Broadcast Room

### Listen

Listening to broadcast from the room.

```js
socket.broadcastRoom.listen(data => {
  // Do Something
});
```

### Send

Send broadcast to the room.

```js
socket.broadcastRoom.send(room, data);
```

### Destroy

Remove listener

```js
socket.broadcastRoom.destroy();
```

## Events

| Event       | KEY          |
| ----------- | ------------ |
| ID Card     | `idCard`     |
| E Signature | `eSignature` |
| Image       | `image`      |
| Info        | `info`       |

### Use event with KEY

```js
socket.KEY.waitingForRequest;
socket.KEY.request;
socket.KEY.send;
socket.KEY.listen;
socket.KEY.destroy;
```

## How to use

### Waiting for request

Listening to another clients call `request`

```js
socket.KEY.waitingForRequest(response => {
  // response will be { clientId, data }
});
```

### Request

Call `request` for trigger `waitingForRequest`.

You can send data to target.

```js
socket.KEY.request(data);
```

### Send

Send Data to Client that requested.

```js
const data = {
  status: 'SUCCESS', // BUSY, ERROR, CANCEL or etc.
  data: '' // any
};
socket.KEY.send(clientId, data);
```

### Listen

Listening to data when client sent.

```js
socket.KEY.listen(data => {
  // Do Something
});
```

### Destroy

Remove all listeners when go out of the page or component will unmount.

```js
socket.KEY.destroy();
```

## Example Usage

### Sender

```js
import trueSmartMessaging from 'true-smart-messaging';

const socket = trueSmartMessaging.connect('http://host.com');

// Room's format is {shop.id}-{counter.id}
const room = '001-1';
const clientName = 'Agent';
socket.joinRoom(room, clientName);

socket.broadcastRoom.listen(() => alert('Broadcast'));

socket.idCard.waitingForRequest(({ clientId, data }) => {
  socket.idCard.send(clientId, {
    status: 'SUCCESS', // BUSY, ERROR
    data: {} // ID Card data
  });
});

// Destroy when go out of the page or component will unmount
socket.idCard.destroy();
```

### Receiver

```js
import trueSmartMessaging from 'true-smart-messaging';

const socket = trueSmartMessaging.connect('http://host.com');

// Room's format is {shop.id}-{counter.id}
const room = '001-1';
const clientName = 'CLIENT-NAME';
socket.joinRoom(room, clientName);

socket.broadcastRoom.listen(() => alert('Broadcast'));

socket.idCard.listen(respone => {
  if (respone.status === 'SUCCESS') {
    doSomething(respone.data);
  }
});

socket.idCard.request();

// Destroy when go out of the page or component will unmount
socket.idCard.destroy();
```

## Socket State Helper

| Name                    | Param required (Object)       |
| ----------------------- | ----------------------------- |
| `getConnectionInRoom()` | `{ state, room }`             |
| `isClientInRoom()`      | `{ state, room, clientName }` |

Example

```js
import trueSmartMessagingHelper from 'true-smart-messaging/helper';

socket.socketState((state, options) => {
  const room = '001-1';
  const clientName = 'CLIENT-NAME';

  const connections = trueSmartMessagingHelper.getConnectionInRoom({ state, room });

  // Do Something
});
```

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