passing-notes-rpc

1.2.3 • Public • Published

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}`
      }

      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()

Readme

Keywords

none

Package Sidebar

Install

npm i passing-notes-rpc

Weekly Downloads

21

Version

1.2.3

License

MIT

Unpacked Size

8.02 kB

Total Files

6

Last publish

Collaborators

  • vinsonchuong