Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nanomessage-rpc

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanomessage-rpc

Tiny rpc on top of nanomessage

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
880
decreased by-14.48%
Maintainers
1
Weekly downloads
 
Created
Source

nanomessage-rpc

Build Status JavaScript Style Guide standard-readme compliant

Tiny :hatched_chick: RPC on top of nanomessage

Install

$ npm install nanomessage-rpc

Usage

const nanomessagerpc = require('nanomessage-rpc')

;(async () => {
  const rpc = nanomessagerpc(socket, opts)

  await rpc
    .action('sum', ({ a, b }) => a + b)
    .action('subtract', ({ a, b }) => a - b)
    .open()

  // from the other rpc socket side
  const result = await rpc.call('sum', { a: 2, b: 2 }) // 4
})()

Also it has an emittery instance to emit events through the socket.

;(async () => {
  const rpc = nanomessagerpc(socket, opts)

  await rpc.open()

  rpc.on('ping', () => {
    console.log('ping')
  })

  // from the other rpc socket side
  const result = await rpc.emit('ping') // 4
})()

And it has support for nanoerror.

const nanomessagerpc = require('nanomessage-rpc')
const nanoerror = require('nanoerror')

const BAD_REQUEST = nanoerror('BAD_REQUEST', 'the request %s is wrong')

;(async () => {
  const rpc = nanomessagerpc(socket, opts)

  await rpc
    .action('badrequest', () => {
      throw new BAD_REQUEST(1)
    })
    .open()

  // from the other rpc socket side
  try {
    const result = await rpc.call('badrequest', { a: 2, b: 2 }) // 4
  } catch (err) {
    // will throw BAD_REQUEST: the request 1 is wrong
  }
})()
const rpc = nanomessagerpc(socket, options)

Create a new nanomessage-rpc.

Options include:

  • timeout: 10 * 1000: Time to wait for the response of a request.
  • concurrency: Infinity: Defines how many requests do you want to run in concurrent.
  • codec: JSON: Defines a compatible codec to encode/decode messages in nanomessage.
rpc.open() -> Promise

Opens nanomessage and start listening for incoming data.

rpc.close() -> Promise

Closes nanomessage and unsubscribe from incoming data.

rpc.action(actionName, handler)

Defines a rpc action and handler for incoming requests.

  • actionName: string: Name of the action.
  • handler: function: Handler, coulb be async.
rpc.actions(actions)

Shortcut to define multiple actions.

  • actions: { actionName: handler, ... }: List of actions.
rpc.call(actionName) -> Promise<Response>

Call an action an wait for the response.

  • actionName: string: Action name.
rpc.on(eventName, handler) -> unsubscribe

Subscribe to a RPC event.

Returns an unsubscribe method.

rpc.once(eventName) -> Promise

Subscribe to a RPC event only once. It will be unsubscribed after the first event.

Returns a promise for the event data when eventName is emitted.

rpc.off(eventName)

Remove a RPC event subscription.

rpc.events(eventName)

Get an async iterator which buffers data each time a RPC event is emitted.

Call return() on the iterator to remove the subscription.

for await (const data of rpc.events('ping')) {
  console.log(data)
  if (disconnected) break
}

Issues

:bug: If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.

Contributing

:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this guideline.

License

MIT © A GEUT project

Keywords

FAQs

Package last updated on 16 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc