Awesome
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
Note: This version of hafas-client-rpc
only works with specific versions of hafas-client
. Check peerDependencies
in package.json
for details.
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).
via WebSockets transport
With this transport, the server part is an actual WebSockets server, and the client connects to it.
// server.js
import http from 'http'
import {createClient as createHafas} from 'hafas-client'
import {profile as vbbProfile} from 'hafas-client/p/vbb/index.js'
import exposeHafasClient from '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
import createRoundRobin from '@derhuerst/round-robin-scheduler'
import createClient from 'hafas-client-rpc/ws/client.js'
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)
Or using the websocat
command-line WebSocket client:
echo 'departures 900000009102' | websocat --jsonrpc 'ws://server-address:3000'
via stdin
/stdout
transport
With this transport, the client spawns the server as a sub-process and sends commands via stdio.
// server.js
import {createClient as createHafas} from 'hafas-client'
import {profile as vbbProfile} from 'hafas-client/p/vbb/index.js'
import exposeViaStdio from 'hafas-client-rpc/stdio/server.js'
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
import createClient from 'hafas-client-rpc/stdio/client.js'
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 -p 'require.resolve("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":{}}}
via UNIX domain sockets
With this transport, both client & server connect to a local TCP socket /tmp/hafas-client-rpc-{version}
.
// server.js
import {createClient as createHafas} from 'hafas-client'
import {profile as vbbProfile} from 'hafas-client/p/vbb/index.js'
import exposeViaSocket from 'hafas-client-rpc/socket/server.js'
const hafas = createHafas(vbbProfile, 'my-awesome-program')
exposeViaSocket(hafas)
// client.js
import createClient from 'hafas-client-rpc/socket/client.js'
createClient((_, hafas) => {
hafas.departures('900000009102')
.then(console.log)
.catch(console.error)
})
via NATS Streaming transport
This 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
import {createClient as createHafas} from 'hafas-client'
import {profile as vbbProfile} from 'hafas-client/p/vbb/index.js'
import exposeViaNatsStreaming from 'hafas-client-rpc/nats-streaming/server.js'
const hafas = createHafas(vbbProfile, 'hafas-client-rpc WebSockets example')
exposeViaNatsStreaming(hafas, (err) => {
if (err) console.error(err)
})
// client.js
import createClient from 'hafas-client-rpc/nats-streaming/client.js'
const pool = createClient((_, hafas) => {
hafas.departures('900000009102')
.then(console.log)
.catch(console.error)
})
Caveats
hafas-client
exposes the used profile ashafasClient.profile
. Because these profiles consist of JavaScript functions, which can't be serialized properly, thehafas-client-rpc
facade does not expose.profile
.
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.