# webudp

> WebUDP based on WebRTC

Latest version **0.0.3** (published 2018-02-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install webudp
pnpm add webudp
yarn add webudp
bun add webudp
```

## 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.0.3 |
| Published | 2018-02-09 |
| First published | 2017-08-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 56.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Ali Najafizadeh |
| Maintainers | alinz |
| Keywords | webudp, webrtc, client, server |

## Links

- npm: https://www.npmjs.com/package/webudp
- Repository: https://github.com/webudp/webudp
- Homepage: https://github.com/webudp/webudp#readme
- Issues: https://github.com/webudp/webudp/issues
- npm.io page: https://npm.io/package/webudp

## Dependencies (1)

- [wrtc](https://npm.io/package/wrtc.md) ^0.0.66

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2018-02-09
- 0.0.2 — 2018-02-09
- 0.0.1 — 2018-02-09
- 0.0.0 — 2017-08-16

## README

```
              _               _
__      _____| |__  _   _  __| |_ __  
\ \ /\ / / _ \ '_ \| | | |/ _` | '_ \
 \ V  V /  __/ |_) | |_| | (_| | |_) |
  \_/\_/ \___|_.__/ \__,_|\__,_| .__/
                               |_|
```

## WebUDP

> NOTE: This is highly experimental and the API will be change

WebUDP is a thin wraper on top of WebRTC. WebUDP tries to mimic the WebSocket API as much as possible at least on the client side.

> Signaling server/client are built on top of noraml http/fetch to make it super portable and light weight.

# Installation

```
yarn add webudp
```

# Usage

WebUDP comes with both server and client class which simplified the complexity of using WebRTC.

#### WebUDPServer

extends WebUDPServer and implement one or more of `onLeave`, `onJoin`, `onError` or `onMessage`.

Also `send(id, data)`, `sendAll(data)` and `close(id)` method are provided by WebUDPServer.

```js
const WebUDPServer = require('webudp/server')

//
class WebUDP extends WebUDPServer {
  constructor(port) {
    super({ port: port })
  }

  onLeave(id) {
    console.log('leave: ', id)
  }

  onJoin(id) {
    console.log('join: ', id)
    this.send(id, 'hello from UDP server')
  }

  onError(id, err) {
    console.log('error: ', id, err)
  }

  onMessage(id, data) {
    console.log('data: ', id, data)

    setTimeout(() => {
      this.send(id, 'hello from UDP server again')
    }, 1000)
  }
}

new WebUDP(8001)
```

#### WebUDPClient

Client side there are four(4) methods to implements. `send(data)` and `close()` are also provided by `WebUDPClient`

```js
class WebUDP extends WebUDPClient {
  constructor(addr) {
    super({ addr: addr })
  }

  onClose() {
    console.log('closed')
  }

  onOpen() {
    console.log('opened')
    this.send('hello udp server')

    setTimeout(() => {
      this.close()
    }, 5000)
  }

  onError(err) {
    console.log('error: ', err)
  }

  onMessage(data) {
    console.log('data', data)

    setTimeout(() => {
      this.send('hello from UDP client again')
    }, 1000)
  }
}

const webUDP = new WebUDP('http://localhost:8001')
```

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