Run RPC over a MessagePort object from a Worker thread (or WebWorker)

Overview

thread-rpc

Run RPC over a MessagePort object from a Worker thread (or WebWorker)

npm install thread-rpc

Usage

First in the parent thread

const ThreadRPC = require('thread-rpc')

// in the main process
const main = new ThreadRPC(workerThreadsInstance)

main.respond('echo', async function (data) {
  return {
    echo: data
  }
})

Then in the child thread

const child = new ThreadRPC(workerThreads.parentPort)

// Send a request
console.log(await child.request('echo', 'world'))

// Send a request synchronously
console.log(child.requestSync('echo', 'world'))

API

const rpc = new ThreadRPC(messagePort, [options])

Create a new instance. messagePort should be a message port instance (ie has the postMessage API). Options include:

{
  syncBufferSize: 4096 // initial size of the sync request shared array buffer
}

rpc.respond(method, async onrequest (params, opts))

Setup a responder for a method. Options include:

{
  transferList, // passed to postMessage's transferList
  sync: bool // true is this is from requestSync - mostly here for debugging
}

const res = await rpc.request(method, params, [transferList])

Send a request to the other side of the instance.

const res = rpc.requestSync(method, params, [transferList])

Same as above except it's synchronous, so it blocks the thread - use with care.

Note that currently this only supports JSON.stringify'able responses or a Buffer/Uint8Array response.

License

MIT

You might also like...

RPC-like client, contract, and server implementation for a pure REST API

RPC-like client, contract, and server implementation for a pure REST API

ts-rest RPC-like client and server helpers for a magical end to end typed experience Introduction ts-rest provides an RPC-like client side interface o

Dec 30, 2022

⚡🚀 Call multiple view functions, from multiple Smart Contracts, in a single RPC query!

ethers-multicall ⚡ 🚀 Call multiple view functions, from multiple Smart Contracts, in a single RPC query! Querying an RPC endpoint can be very costly

Dec 30, 2022

an open-source package to make it easy and simple to work with RabbitMQ's RPC ( Remote Procedure Call )

an open-source package to make it easy and simple to work with RabbitMQ's RPC ( Remote Procedure Call )

RabbitMQ Easy RPC (Remote Procedure Call ) The Node.js's RabbitMQ Easy RPC Library rabbitmq-easy-RPC is an easy to use npm package for rabbitMQ's RPC

Sep 22, 2022

A MITM cache between RPCs and a a dAPP. Useful to allow for better performance on a public RPC node

better-cosmos-rpcs A cheaper way to allow for public RPCs as a service WITHOUT scaling issues. No need to rate limit either. How it is done: User GET

Nov 19, 2022

An interactive Bitcoin tutorial for orange-pilled beginners. Illustrates technical Bitcoin concepts using JavaScript and some Bitcoin Core RPC commands. Programming experience is helpful, but not required.

An interactive Bitcoin tutorial for orange-pilled beginners. Illustrates technical Bitcoin concepts using JavaScript and some Bitcoin Core RPC commands. Programming experience is helpful, but not required.

Try Bitcoin Try Bitcoin is an interactive Bitcoin tutorial inspired by and forked from Try Regex, which is inspired by Try Ruby and Try Haskell. It il

Nov 25, 2022

A Cloudflare Worker for fetching data from Twitter User API.

cloudflare-worker-twitter This is a Cloudflare Worker for fetching data from Twitter User API. ❔ About this project I created this Worker to hide my A

Oct 1, 2022

👄 My simple worker for short links

👄 My simple worker for short links I wanted a quick-and-easy way to create links on my domain, so I turned to Cloudflare Workers. All routes availabl

Apr 15, 2022

A cloudflare worker to use the user-agent for ~~rickrolling~~ without a discord embed

nextcord.gay A cloudflare worker to use the user-agent for rickrolling without a discord embed Build npm run build Find the output in ./dist/worker.mj

Oct 4, 2022

Cloudflare worker function to update github bio automatically with leetcode and codeforces profile stats 🚀

Stats check 🔢 Cloudflare worker function to update github bio automatically with leetcode and codeforces profile stats 🚀 You can see it in action he

Feb 15, 2022
Owner
Mathias Buus
Rød grød med fløde
Mathias Buus
Patronum: Ethereum RPC proxy that verifies RPC responses against given trusted block hashes

Patronum Ethereum RPC proxy that verifies RPC responses against given trusted block hashes. Currently, most of the DAPPs and Wallets interact with Eth

null 14 Dec 7, 2022
A Node.js client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP-J protcols.

OCPP-RPC A client & server implementation of the WAMP-like RPC-over-websocket system defined in the OCPP-J protcols (e.g. OCPP1.6J and OCPP2.0.1J). Re

Mikuso 14 Dec 30, 2022
HN Notifier is a chrome extension that shows the number of unread comments to your HackerNews thread

HN Notifier is a chrome extension that shows the number of unread comments to your HackerNews thread. One of the issues with HackerNews is that it is difficult to know when someone has replied to your post or comment, thus by missing an opporunity to continue the conversation.

Engagespot 12 Oct 22, 2022
Multithread emulator. The wrun allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file

wrun This lib allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file. This means tha

Felippe Regazio 9 Nov 5, 2022
Map over an object in a preorder or postoder depth-first manner

obj-walker Walk objects like this guy. Map over an object in a preorder or postoder depth-first manner. Also, provides functions for serializing and d

David Sargeant 3 Jun 2, 2022
Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Vineeth.TR 16 Dec 6, 2022
Run a command, watch the filesystem, stop the process on file change and then run the command again...

hubmon Run a command, watch the filesystem, stop the process on file change and then run the command again... Install You can install this command lin

Hubert SABLONNIÈRE 7 Jul 30, 2022
Collection of JSON-RPC APIs provided by Ethereum 1.0 clients

Ethereum JSON-RPC Specification View the spec The Ethereum JSON-RPC is a collection of methods that all clients implement. This interface allows downs

null 557 Jan 8, 2023
my ethereum RPC node setup & notes

UBUNTU RPC (scaffold-rpc) sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install ethereum = created geth script = g

Austin Griffith 10 Jul 4, 2022