emitrpc
v0.0.26
Published
Emit server events via HTTP and JSON RPC
Downloads
1
Readme
Emit RPC
Event Emitter Implementation for the JSON-RPC 2.0 Specification
Install
$ npm i emitrpc
or
$ yarn add emitrpc
Basics
// server.js
import { EventEmitter, middleware } from 'emitrpc'
const emitter = new EventEmitter
emitter.on('foo', (req, res) => {
res.write({ error: false, results: { name: req.params.name } })
})
//make a server with the middleware
this.server = http.createServer(middleware(emitter))
this.server.listen(3000)
// client.js
import { RPCEmitter } from 'emitrpc'
const emitter = new RPCEmitter('http://127.0.0.1:3000/emitrpc')
const res = await clientEmitter.emit('company-detail', { name: 'bar' })
console.log(res.body.results.name) //--> bar
RPCEmitter
RPCEmitter
extends EventEmitter
new RPCEmitter(endpoint: string, options: object)
Options
- fetch - The
fetch()
to use; defaults towindow.fetch
- id - number; Used to make a unique ID; defaults to
1
- key - string; Used to make a unique ID; defaults to a random short ID
- method - string; Options:
GET
,POST
,PUT
,DELETE
, etc. - mode - string; Options:
no-cors
,cors
,same-origin
- cache - string; Options:
default
,no-cache
,reload
,force-cache
,only-if-cached
- credentials - string; Options:
include
,same-origin
,omit
- headers - A key value hash,
- redirect - string; Options:
manual
,follow
,error
- referrer - string; Options:
no-referrer
,client