# plug-socket

> Simple plug.dj WebSocket server interaction.

Latest version **1.0.0** (published 2018-05-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install plug-socket
pnpm add plug-socket
yarn add plug-socket
bun add plug-socket
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-05-03 |
| First published | 2015-05-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 25.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Renée Kooi |
| Maintainers | burkes, goto-bus-stop |
| Keywords | plug, plug.dj, websocket, ws |

## Links

- npm: https://www.npmjs.com/package/plug-socket
- Repository: https://github.com/goto-bus-stop/plug-socket
- Issues: https://github.com/goto-bus-stop/plug-socket/issues
- npm.io page: https://npm.io/package/plug-socket

## Dependencies (1)

- [ws](https://npm.io/package/ws.md) ^5.0.0

## 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.0.0 (latest) — 2018-05-03
- 0.6.1 — 2018-05-03
- 0.6.0 — 2017-07-12
- 0.5.0 — 2017-05-18
- 0.4.0 — 2017-03-20
- 0.3.0 — 2017-01-03
- 0.2.1 — 2016-12-02
- 0.2.0 — 2016-12-02
- 0.1.1 — 2016-11-27
- 0.1.0 — 2016-06-02
- 0.0.2 — 2015-05-30
- 0.0.1 — 2015-05-30

## README

plug-socket
===========

Simple plug.dj WebSocket EventEmitter library.

[![NPM](https://nodei.co/npm/plug-socket.png?downloads)](https://nodei.co/npm/plug-socket)

## Usage

```javascript
const plugSocket = require('plug-socket')
const authToken = '(...)' // get one by GET-ing https://plug.dj/_/auth/token

let socket = plugSocket(authToken)
// events will be fired on "socket" for every incoming plug.dj message
socket.on('chat', msg => log(`<${msg.un}>  ${msg.message}`))
socket.on('userJoin', msg => log(`  * ${msg.un} joined the room`))
socket.on('plugMaintenanceAlert', () => log('#ded soon… ×.×'))
```

## API

### let socket = plugSocket(?authToken)

Sets up a WebSocket connection to plug.dj. If the auth token is given, it also
sends an "auth" message once the connection is open. Otherwise, you'll have to
send that yourself.

`socket` is a [WebSocket](https://github.com/websockets/ws) connection instance
with a few extra methods and a bunch of extra events.

### socket.auth(authToken)

Sends an auth token. You should only call this once, and only if you did not
pass one to the `plugSocket()` call.

You can obtain an auth token by logging in to plug.dj using something like
[plug-login](https://github.com/goto-bus-stop/plug-login), or by manually
sending a GET request to https://plug.dj/_/auth/token.

```javascript
const plugSocket = require('plug-socket')

// Using `plug-login`'s authToken option:
const plugLogin = require('plug-login')
plugLogin(myEmail, myPassword, { authToken: true }).then((result) => {
  const sock = plugSocket(result.token)
})

// Or manually, with a cookie stored in `mySessionCookie`:
const got = require('got')
got('https://plug.dj/_/auth/token', {
  json: true,
  // Make
  headers: { cookie: mySessionCookie }
}).then((response) => {
  const authToken = response.body.data[0]
  const sock = plugSocket(authToken)
})
```

### socket.chat(message)

Sends a chat message to the current room. Make sure to join a room first by
sending a POST request to https://plug.dj/_/rooms/join:

```javascript
got.post('https://plug.dj/_/rooms/join', {
  json: true,
  headers: {
    cookie: mySessionCookie,
    'content-type': 'application/json'
  },
  body: JSON.stringify({
    slug: 'my-room-slug'
  })
}).then((response) => { /* joined! */ })
```

### Events

Aside from the standard WebSocket events, `plug-socket` also emits different
events for all plug.dj message types. These are:

```javascript
[ "ack", "advance", "ban", "banIP", "chat", "chatDelete", "djListCycle"
, "djListLocked", "djListUpdate", "earn", "sub", "cash", "gift", "floodChat"
, "floodAPI", "friendRequest", "friendAccept", "gifted", "grab", "killSession"
, "modBan", "modAddDJ", "modRemoveDJ", "modMoveDJ", "modMute", "modSkip"
, "modStaff", "nameChanged", "nameChangedRoom", "notify", "playlistCycle"
, "plugMaintenance", "plugMaintenanceAlert", "plugMessage", "plugUpdate"
, "rateLimit", "roomNameUpdate", "roomDescriptionUpdate", "roomWelcomeUpdate"
, "roomMinChatLevelUpdate", "skip", "userJoin", "userLeave", "userUpdate"
, "vote" ]
```

Plug.dj events receive two arguments, `param` and `slug`. `param` is *usually*
an object, or `undefined` for some events. The `slug` parameter contains the
current room slug or "dashboard". When you switch rooms, sometimes you'll keep
receiving a few events from your previous room, so the `slug` parameter allows
you to filter those. You won't have to care for it if your app doesn't switch
rooms much.

```javascript
socket.on('chat', (param, slug) => {
  log(`${slug}: receiving`, param)
})
```

You can also handle _every_ plug.dj event by adding an "action" listener:

```javascript
socket.on('action', (type, param, slug) => {
  // `type` is one of the events listed above.
  log(`${slug}: receiving a "${type}" event with`, param)
})
```

Most events are documented in more detail in the [PlugCommunity Documentation][plugcommunity docs]
repository.

## Tests

Tests use `mocha`. All tests depend on plug.dj being online and reachable, so
you might get test failures if it's slow, or in maintenance mode, or shut down
for a few months.

## License

[MIT][license]

[plugcommunity docs]: https://github.com/plugcommunity/documentation/tree/master/api/events/backend_events/
[license]: ./LICENSE

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