Skip to main content

This module offers a simple function to connect the disjointed sending and receiving of messages between two parties.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
2 months ago

Requester

Requester is a simple lib offering a method to connect disconnected communication between two environments like workers for example. Basically joining the connections between sending and receiving information.

Example

The below example connects a Client and a ShareWorker together so the Client can request information from the ShareWorker and await a response.

import { createRequester } from '@doctor/requester'

const port = new SharedWorker('path').port

const { post, request, onMessage } = createRequester<string>(
	payload => port.postMessage(payload), // Handle Sending Messages
	data => console.log(data) // Handle Responding to Messages
)

port.onmessage = ({ data: { data: WorkerMessage } }) => onMessage(data)

console.log(await request('123')) // 123
import createRequester from '@doctor/requester'

const { onMessage } = createRequester<number, MessagePort>(
	(payload, port) => port.postMessage(payload) // Handle Sending Messages
	data => Number(data) // Handle Responding to Messages
)

self.onconnect = function({ ports: [port] }: MessageEvent) {
	port.onmessage = ({ data: { data: WorkerMessage } }) => onMessage(data, port)
}
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @doctor/requester

Import symbol

import * as mod from "@doctor/requester";

Add Package

npx jsr add @doctor/requester

Import symbol

import * as mod from "@doctor/requester";

Add Package

yarn dlx jsr add @doctor/requester

Import symbol

import * as mod from "@doctor/requester";

Add Package

pnpm dlx jsr add @doctor/requester

Import symbol

import * as mod from "@doctor/requester";

Add Package

bunx jsr add @doctor/requester

Import symbol

import * as mod from "@doctor/requester";