demande
v0.4.0
Published
client-side implementation of json-rpc 2.0 over websockets
Downloads
10
Maintainers
Readme
#demande
a client-side implementation of JSON-RPC 2.0 over websockets
Installation
$ npm install demande
Usage
import rpc from 'demande'
const ws = new WebSocket('ws://localhost:8080')
ws.onmessage = e => rpc.resolve(e.data)
const trueFriend = [
"she gives what is hard to give",
"she does what is hard to do",
"she endures what is hard to endure",
"she reveals their secrets to ye",
"she keeps ye secrets",
"when misfortunes strike, she doesn't abandon ye",
"when ye're down and out, she doesn't look down on ye"
]
function fetchTrueFriends () {
rpc.fetch('fetchFriends', trueFriend, friends => {
learnFrom(friends)
})(ws)
}
API
.fetch()
Builds a JSON-RPC request string with a unique id and sends it over the provided sockets. Stores the request with it's callback so it can be retrieved when the response comes back.
// rpc.fetch(method: str, params: obj, cb: function) -> (socket: WebSocket) -> null
rpc.fetch('fetchFriends', {respectable: true}, friends => {
learnFrom(friends)
})(ws)
.resolve()
Parses every web socket message and executes any callback associated with the response.
// rpc.resolve(data: str) -> null
ws.onmessage( e => {
// e.data == "{ jsonrpc: "2.0", id: H3FWE12, result: {} }"
rpc.resolve(e.data)
})
.getPending()
Returns a list of pending requests. Used mainly for testing purposes. Read-only.
// rpc.getPending() -> arr