# fpm-plugin-socket

> FPM socket plugin

Latest version **1.1.1** (published 2019-04-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install fpm-plugin-socket
pnpm add fpm-plugin-socket
yarn add fpm-plugin-socket
bun add fpm-plugin-socket
```

## 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.1.1 |
| Published | 2019-04-22 |
| First published | 2018-04-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 28.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | YFsoftcom |
| Maintainers | yfsoftcom |
| Keywords | socket |

## Links

- npm: https://www.npmjs.com/package/fpm-plugin-socket
- npm.io page: https://npm.io/package/fpm-plugin-socket

## Dependencies (3)

- [q](https://npm.io/package/q.md) ^1.5.1
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.4

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2019-04-22
- 1.1.0 — 2019-04-22
- 1.0.10 — 2018-12-03
- 1.0.9 — 2018-11-28
- 1.0.8 — 2018-08-24
- 1.0.7 — 2018-08-21
- 1.0.6 — 2018-08-21
- 1.0.5 — 2018-08-11
- 1.0.4 — 2018-08-11
- 1.0.3 — 2018-08-11
- 1.0.2 — 2018-07-31
- 1.0.1 — 2018-07-30
- 1.0.0 — 2018-06-07
- 0.0.12 — 2018-04-23
- 0.0.11 — 2018-04-23
- … 10 more at https://npm.io/package/fpm-plugin-socket/versions

## README

# fpm-plugin-socket

A plugin for Socket, It's only for a few datas( Normally < 20 bytes ). 

We use it to build IOT project.

Transform with byte array like hex.

~Support `CRC16` data compare.~


## Basic Info
- Run Action Hook Name: `BEFORE_SERVER_START`
- ExtendModule Name: `socket`
- Exception
  - [x] Nope
- `getDependencies()`
  - [x] `[]`
- The Reference Of The `Bind()` Method
  An BizModule Object Contains those Functions
  - [ ] `broadcast`
  - [ ] `send`
  - [ ] `getOnlineDevice`
  - [ ] `isOnline`

## Usage
- SetEncoder/SetDecoder

  - setDataDecoder
    it must be return a Object{id, data}, and some crc16 compare, return `undefined` if crc16 error
    
    ```javascript
    socketServer.setDataDecoder((src) => {
      // the last 2 bytes is the crc16 data
      let data = _.dropRight(src, 2)
      data = Buffer.from(data, 'hex')

      let id = Buffer.from(_.take(data, 2), 'hex').toString('hex')
      return {id, data}
    })
    ```
  - setDataEncoder
    
    Make sure return an Buffer Object

- Broadcast message

  `fpm.execute('socket.broadcast', message!Object, channel?Array, ids?Array) => Array`
  - it always return a resolved Promise with the counter of success broadcast messages

- Send message to The client

  `fpm.execute('socket.send', id!String, message!HexString) => Promise`
  - it return an reject if send error

- Add channel for clients

  `fpm.execute('socket.addChannel', channel!String, ids!Array) => Array`
  - it return an array of addChannel success ok.

- getOnlineDevice

  `fpm.execute('socket.getOnlineDevice', message!Object) => Object`
  - it returns the online devices

- isOnline

  `fpm.execute('socket.isOnline', id!String) => Number!0/1`
  - it returns 1 which use the id is online

- getClient

  `fpm.execute('socket.getClient', id!String) => SocketClient`
  - it returns socketClient of the online device with id

- Subscribe Event To Receive Message 
  - [ ] `##socket/receive`
    ```javascript
	fpm.subscribe('#socket/receive', (topic, message) => {
		console.info(message)
	})
	```
  - [ ] `#socket/connect`
    ```javascript
	fpm.subscribe('#socket/connect', (topic, message) => {
		console.info(message);
  })
  /*
  { id: '0102',
  ip: '127.0.0.1',
  port: 41510,
  channel: [],
  tag: 'UN_TAGED',
  alias: 'UN_ALIAS',
  extend: {} }
  */
	```
  - [ ] `#socket/decode/error`
    ```javascript
	fpm.subscribe('#socket/decode/error', (topic, data) => {
		console.info(data)
	})
	```
  - [ ] `#socket/close`
    ```javascript
	fpm.subscribe('#socket/close', (topic, client) => {
		console.info(client)
	})
	```
  - [ ] `#socket/error`
    ```javascript
	fpm.subscribe('#socket/error', (topic, error) => {
		console.info(error)
	})
	```

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