# @lucets/registry

> Client registry for @lucets/luce

Latest version **0.2.0** (published 2021-02-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @lucets/registry
pnpm add @lucets/registry
yarn add @lucets/registry
bun add @lucets/registry
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2021-02-23 |
| First published | 2021-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Michiel van der Velde |
| Maintainers | michielvdvelde |
| Keywords | luce, ws, websocket |

## Links

- npm: https://www.npmjs.com/package/@lucets/registry
- Repository: https://github.com/lucets/registry
- Homepage: http://github.com/lucets/registry
- Issues: https://github.com/lucets/registry/issues
- npm.io page: https://npm.io/package/@lucets/registry

## Dependencies (1)

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

## 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

- 0.2.0 (latest) — 2021-02-23
- 0.1.0 — 2021-02-22

## README

# @lucets/registry

Register WebSocket clients and manage their info.

The main component is a `Registry` interface which can be implemented
in your own registries.
The accompanying `LocalRegistry` is an in-memory registry which can be
used as a reference implementation.

Developed for use in combination with [@lucets/luce](https://github.com/lucets/luce),
but is in no way dependent on it.

## Install

```
npm i @lucets/registry
```

## Example

An example in combination with luce.

```ts
'use strict'

import Application, { WebSocketError } from '@lucets/luce'
import { LocalRegistry } from '@lucets/registry'
import { nanoid } from 'nanoid/async'

const app = new Application()
const registry = new LocalRegistry()

// Set an ID
// Set a flag to delete the peer from the
// registry once the connection has closed
app.useUpgrade('pre', async (ctx, next) => {
  ctx.state.id = await nanoid()
  ctx.state.deleteOnClose = true
  return next()
})

app.useUpgrade('post', async (ctx, next) => {
  // Create the ID if it doesn't exist yet
  if (!await registry.exists(ctx.state.id)) {
    await registry.create(ctx.state.id)
  }

  // Register the socket with the registry
  await registry.register(ctx.state.id, ctx.socket)

  ctx.socket.once('close', async () => {
    // Unregister the ID
    await registry.unregister(ctx.state.id)
  
    // Delete the ID if the flag is set
    if (ctx.state.deleteOnClose) {
      await registry.delete(ctx.state.id)
    }
  })

  return next()
})

app.useMessage(async (message, ctx, next) => {
  // Update client info
  if (message.cmd === 'update-info') {
    await registry.update(ctx.state.id, message.info)
  }
})
```

## License

Copyright 2021 [Michiel van der Velde](https://michielvdvelde.nl).

This software is licensed under [the MIT License](LICENSE).

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