Giter Site home page Giter Site logo

k-rpc-socket's Introduction

k-rpc-socket

Low level implementation of the k-rpc network layer that the BitTorrent DHT uses. Mostly extracted from the bittorrent-dht module on npm into its own repo.

npm install k-rpc-socket

build status

Usage

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}, {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:

{
  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, callback)

Call this to bind to a specific port. 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

k-rpc-socket's People

Contributors

mafintosh avatar feross avatar xuset avatar

Watchers

James Cloos avatar Aaron Hill avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.