Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
dho-rpc-transport-webworker
Advanced tools
Readme
Offload intensive calculations to WebWorker with just several lines of code.
dho-rpc is a tiny transport agnostic JSON-RPC 2.0 client and server which can work both in NodeJs, Browser, Electron etc.
This is WebWorker based transport but there are many more transports.
What is the reason of using JSON-RPC for WebWorkers?
People usually send data to WebWorker in a form like {id, method, params}
in JSON format via postMessage. As a result they expect {id, result}
. And this is what JSON RPC is. id
is required when you send several request and need to map results and requests.
So, why to write own solution every time? This transport allows you use JSON RPC for calling methods in WebWorkers. Moreover, it will handle all of edge-cases like errors processing, timeouts, batch calls, bidirectional communication etc.
dho-rpc has zero dependencies and is a very small library.
worker.js
import expose from 'dho-rpc-transport-webworker/expose';
const sum = (a, b) => a + b;
const multiply = (a, b) => a * b;
expose({ sum, multiply }).catch(console.error);
index.js
import createClient from 'dho-rpc-transport-webworker/createClient';
async function main() {
const client = createClient(new Worker('worker.js'));
const result = await client.sum(2, 3);
}
main().then(console.log, console.error);
You can pass extra dho-rpc options to createClient as second parameter.
For example
const client = createClient(new Worker('worker.js'), {
requestTimeout: 1000
});
See examples/simple-send-work-to-webworker
See examples/send-work-to-webworker
Each side works as client and server the same time.
FAQs
Easy way to communicate with webworker using dho-rpc (JSON RPC library)
The npm package dho-rpc-transport-webworker receives a total of 22 weekly downloads. As such, dho-rpc-transport-webworker popularity was classified as not popular.
We found that dho-rpc-transport-webworker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.