README
hafas-client-rpc
Make JSON-RPC calls to hafas-client
via
- WebSockets – Supports reconnecting and load-balancing via
websocket-pool
. stdin
/stdout
- UNIX domain sockets
- NATS Streaming
Installation
npm install hafas-client-rpc
Usage
hafas-client-rpc
has multiple transports. Each of them has a client part (which sends commands to make HAFAS calls) and a server (which executes the HAFAS calls).
WebSockets transport
viaWith this transport, the server part is an actual WebSockets server, and the client connects to it.
// server.js
const http = require('http')
const createHafas = require('hafas-client')
const vbbProfile = require('hafas-client/p/vbb')
const exposeHafasClient = require('hafas-client-rpc/ws/server')
const httpServer = http.createServer()
httpServer.listen(3000)
const hafas = createHafas(vbbProfile, 'my-awesome-program')
const server = exposeHafasClient(httpServer, hafas)
// client.js
const createRoundRobin = require('@derhuerst/round-robin-scheduler')
const createClient = require('hafas-client-rpc/ws/client')
const pool = createClient(createRoundRobin, [
'ws://server-address:3000'
// pass more addresses here if you want
], (_, hafas) => {
hafas.departures('900000009102')
.then(console.log)
.catch(console.error)
})
pool.on('error', console.error)
stdin
/stdout
transport
via With this transport, the client spawns the server as a sub-process and sends commands via stdio.
// server.js
const createHafas = require('hafas-client')
const vbbProfile = require('hafas-client/p/vbb')
const exposeViaStdio = require('hafas-client-rpc/stdio/server')
const hafas = createHafas(vbbProfile, 'my-awesome-program')
exposeViaStdio(hafas, (err) => {
console.log('hafas-client-rpc server ready')
})
Creating a client in Node.js doesn't make sense, because you could just use hafas-client
directly. You would usually write the client in another language. For demonstration purposes, a Node client:
// client.js
const createClient = require('hafas-client-rpc/stdio/client')
createClient('path/to/stdio/server.js', (_, hafas) => {
hafas.departures('900000009102')
.then(console.log)
.catch(console.error)
})
with other languages
Spawn the stdio RPC server as a sub process of your script:
node node_modules/hafas-client-rpc/stdio/simple-server.js
Send JSON-RPC 2.0 calls via stdin
:
{"jsonrpc":"2.0","id":"1","method":"departures","params":["900000009102"]}
On success, you will receive the result via stdout
:
{"jsonrpc":"2.0","id":"1","result":[{"tripId":"1|32623|3|86|8122018", …}]}
If an error occurs, you will receive it via stderr
:
{"jsonrpc":"2.0","id":"1","error":{"message":"station ID must be a valid IBNR.","code":0,"data":{}}}
UNIX domain sockets
viaWith this transport, both client & server connect to a local TCP socket /tmp/hafas-client-rpc-{version}
.
// server.js
const createHafas = require('hafas-client')
const vbbProfile = require('hafas-client/p/vbb')
const exposeViaSocket = require('hafas-client-rpc/socket/server')
const hafas = createHafas(vbbProfile, 'my-awesome-program')
exposeViaSocket(hafas)
// client.js
const createClient = require('hafas-client-rpc/socket/client')
createClient((_, hafas) => {
hafas.departures('900000009102')
.then(console.log)
.catch(console.error)
})
NATS Streaming transport
viaThis transport relies on NATS streaming channels. This allows you to have a pool of servers where an individual server can go offline at any time, as the channel will persist all RPC requests until they're taken care of. The transport uses two durable channels (one for RPC requests, the other for responses).
// server.js
const createHafas = require('hafas-client')
const vbbProfile = require('hafas-client/p/vbb')
const exposeViaNatsStreaming = require('hafas-client-rpc/nats-streaming/server')
const hafas = createHafas(vbbProfile, 'hafas-client-rpc WebSockets example')
exposeViaNatsStreaming(hafas, (err) => {
if (err) console.error(err)
})
// client.js
const createClient = require('hafas-client-rpc/nats-streaming/client')
const pool = createClient((_, hafas) => {
hafas.departures('900000009102')
.then(console.log)
.catch(console.error)
})
Related
hafas-client
– JavaScript client for HAFAS public transport APIs.
Contributing
If you have a question or have difficulties using hafas-client-rpc
, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.