passing-notes-rpc
v1.2.3
Published
Simple communication between browser and server
Downloads
66
Readme
passing-notes-rpc
Simple communication between browser and server
Usage
Install passing-notes-rpc by running:
yarn add passing-notes-rpc
Then, compose it with other middleware:
import {compose, Logger} from 'passing-notes'
import {serveRpc} from 'passing-notes-rpc'
import serveUi from 'passing-notes-ui'
const logger = new Logger()
export default compose(
serveRpc({
logger,
actions: {
echo(text) {
return `echo: ${text}`
}
async *subscribe() {
yield 'One!'
yield 'Two!'
yield 'Three!'
}
}
}),
serveUi({path: './ui', logger}),
() => () => ({status: 404})
)
These actions can then be called in the browser:
// ui/index.html
<!doctype html>
<meta charset="utf-8">
<script type="module" src="/index.js"></script>
// ui/index.js
import {makeRpcClient} from 'passing-notes-rpc'
const client = makeRpcClient()
async function run() {
console.log(await client.echo('Hello!'))
// 'echo: Hello!'
for await (const message of await client.subscribe()) {
console.log(message)
}
// One!
// Two!
// Three!
}
run()