# k-rpc-socket

> Low level implementation of the k-rpc network layer that the BitTorrent DHT uses

Latest version **1.11.1** (published 2019-08-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install k-rpc-socket
pnpm add k-rpc-socket
yarn add k-rpc-socket
bun add k-rpc-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.11.1 |
| Published | 2019-08-10 |
| First published | 2015-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Mathias Buus |
| Maintainers | feross, mafintosh |

## Links

- npm: https://www.npmjs.com/package/k-rpc-socket
- Repository: https://github.com/mafintosh/k-rpc-socket
- Issues: https://github.com/mafintosh/k-rpc-socket/issues
- npm.io page: https://npm.io/package/k-rpc-socket

## Dependencies (4)

- [bencode](https://npm.io/package/bencode.md) ^2.0.0
- [chrome-dns](https://npm.io/package/chrome-dns.md) ^1.0.0
- [chrome-net](https://npm.io/package/chrome-net.md) ^3.3.2
- [chrome-dgram](https://npm.io/package/chrome-dgram.md) ^3.0.2

## Recent versions

- 1.11.1 (latest) — 2019-08-10
- 1.11.0 — 2019-08-10
- 1.10.0 — 2019-08-08
- 1.9.0 — 2019-08-06
- 1.8.0 — 2018-02-28
- 1.7.2 — 2017-12-02
- 1.7.1 — 2017-06-23
- 1.7.0 — 2017-06-22
- 1.6.2 — 2017-02-08
- 1.6.1 — 2017-01-16
- 1.6.0 — 2016-08-07
- 1.5.2 — 2016-05-30
- 1.5.1 — 2016-04-05
- 1.5.0 — 2016-02-07
- 1.4.5 — 2016-02-03
- … 12 more at https://npm.io/package/k-rpc-socket/versions

## README

# k-rpc-socket

Low level implementation of the k-rpc network layer that the [BitTorrent DHT](http://www.bittorrent.org/beps/bep_0005.html) uses.
Mostly extracted from the [bittorrent-dht](https://github.com/feross/bittorrent-dht) module on npm into its own repo.

```
npm install k-rpc-socket
```

[![build status](http://travis-ci.org/mafintosh/k-rpc-socket.svg?branch=master)](http://travis-ci.org/mafintosh/k-rpc-socket)

## Usage

``` js
var rpc = require('k-rpc-socket')

var socket = rpc()

socket.on('query', function (query, peer) {
  socket.response(peer, query, {echo: query.a})
})

socket.bind(10000, function () {
  var anotherSocket = rpc()
  anotherSocket.query({host: '127.0.0.1', port: 10000}, {q: 'echo', a: {hello: 'world'}}, function (err, response) {
    console.log(response.r) // prints {echo: {hello: Buffer('world')}}
  })
})
```

## API

#### `var socket = rpc([options])`

Create a new k-rpc-socket. Options include:

``` js
{
  timeout: queryTimeout, // defaults to 2s
  socket: optionalUdpSocket,
  isIP: optionalBooleanFunction
}
```

#### `socket.send(peer, message, [callback])`

Send a raw message. The callback is called when the message has been flushed from the socket.

#### `var id = socket.query(peer, query, [callback])`

Send a query message. The callback is called with `(err, response, peer, request)`.
You should set the method name you are trying to call as `{q: 'method_name'}` and query data as `{a: someQueryData}`.

The query method returns a query id. You can use this id to cancel the query using the `.cancel` method.

#### `socket.cancel(id)`

Cancel a query. Will call the corresponding query's callback with an error indicating that it was cancelled.

#### `socket.response(peer, query, response, [callback])`

Send a response to a query. The callback is called when the message has been flushed from the socket.

#### `socket.error(peer, query, error, [callback])`

Send an error reploy to a query. The callback is called when the message has been flushed from the socket.

#### `socket.inflight`

Integer representing the number of concurrent queries that are currently pending.

#### `socket.destroy()`

Destroys and unbinds the socket

#### `socket.bind([port], [address], [callback])`

Call this to bind to a specific port. If port is not specified or is 0, the operating system
will attempt to bind to a random port. If address is not specified, the operating system will
attempt to listen on all addresses.

If you don't call this a random free port will be chosen.

#### `socket.on('query', query, peer)`

When a query is received a `query` event is emitted with the query data and a peer object representing the querying peer.

#### `socket.on('warning', error)`

Emitted when a non fatal error has occured. It is safe to ignore this.

#### `socket.on('error', error)`

Emitted when a fatal error has occured.

## License

MIT

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