passing-notes-rpc

Simple communication between browser and server

Usage no npm install needed!

<script type="module">
  import passingNotesRpc from 'https://cdn.skypack.dev/passing-notes-rpc';
</script>

README

passing-notes-rpc

npm CI Status dependencies Status devDependencies Status

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}`
      }
    }
  }),
  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() {
  const result = await client.echo('Hello!')
  console.log(result) // 'echo: Hello!'
}

run()