npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hafas-client-rpc

v5.0.1

Published

Make JSON-RPC calls to hafas-client via WebSockets, stdio, UNIX domain sockets & NATS streaming.

Downloads

32

Readme

hafas-client-rpc

Make JSON-RPC calls to hafas-client via

npm version build status ISC-licensed support me via GitHub Sponsors chat with me on Twitter

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 as hafasClient.profile. Because these profiles consist of JavaScript functions, which can't be serialized properly, the hafas-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.