Socket
Socket
Sign inDemoInstall

instant-api

Package Overview
Dependencies
81
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    instant-api

Like instant soup but API. JSON-RPC2 flavor with Websockets and HTTP.


Version published
Maintainers
1
Created

Readme

Source

Instant API

Like instant soup but API. JSON-RPC2 flavor with Websockets and HTTP.

💾 Install

npm i -s instant-api

📡 Expose task 'bringBeer' at port 3000

var tasks = {
  'bringBeer': require('./tasks/bring-beer')
}
require('instant-api')(tasks ,{ port: process.env.PORT || 3000 })

🤖 tasks/bring-beer.js

module.exports = function (rpc) {
  
  // use parameters
  console.log(rpc.params)
  
  // return result
  rpc.sendResult('cheers')
  
  // return param error
  //rpc.sendParamsError('Missing parameter ...')
  
  // return custom error
  //rpc.sendError('Boom')
  
  // use in promise chains
  // rawQuery(query).then(rpc.sendResult).catch(rpc.sendError)
  
}

📣 Call your method...

var message = {
  method: 'bringBeer',
  params: { temperature: 'cold' },
  jsonrpc: '2.0',
  id: Math.round(Math.random()*1e20)
}

// ... from a browser using HTTP
fetch('localhost:3000', {
  method: 'POST', body: JSON.stringify( message )
}).then(function(response){
  return response.json()
}).then(function(body){
  console.log(JSON.parse(body.result))
}).catch(console.error)
 
// ... from a browser using Websockets
var ws = new WebSocket('ws://localhost:3000')
ws.onopen = function () {
  ws.send( JSON.stringify(message) )
}
ws.onmessage = function (event) {
  console.log(JSON.parse(event.data))
}

// ... from another server
require('request').post({
  url: 'http://localhost:3000',
  json: message
}, function (error, response, body) {
  if (!error && response.statusCode === 200) {
    console.log(body.result)
  } else {
    console.error(error || body)
  }
})

FAQs

Last updated on 03 Jun 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc