{"name":"birpc","type":"module","version":"2.2.0","description":"Message based Two-way remote procedure call","author":{"name":"Anthony Fu","email":"anthonyfu117@hotmail.com"},"license":"MIT","funding":"https://github.com/sponsors/antfu","homepage":"https://github.com/antfu/birpc#readme","repository":{"type":"git","url":"git+https://github.com/antfu/birpc.git"},"bugs":{"url":"https://github.com/antfu/birpc/issues"},"keywords":["rpc","messages"],"sideEffects":false,"exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","devDependencies":{"@antfu/eslint-config":"^4.1.1","@antfu/ni":"^23.3.1","@types/node":"^22.13.1","bumpp":"^10.0.1","eslint":"^9.19.0","tsx":"^4.19.2","typescript":"^5.7.3","unbuild":"^3.3.1","vite":"^6.0.11","vitest":"^3.0.5"},"scripts":{"build":"unbuild","dev":"unbuild --stub","lint":"eslint .","release":"bumpp && pnpm publish","start":"tsx src/index.ts","typecheck":"tsc --noEmit","test":"vitest"},"_id":"birpc@2.2.0","_integrity":"sha512-1/22obknhoj56PcE+pZPp6AbWDdY55M81/ofpPW3Ltlp9Eh4zoFFLswvZmNpRTb790CY5tsNfgbYeNOqIARJfQ==","_resolved":"/private/var/folders/30/nymxcyb909ggq2j5lwn7b_600000gn/T/b55696ed29c57e07af76d2c68fca38a9/birpc-2.2.0.tgz","_from":"file:birpc-2.2.0.tgz","_nodeVersion":"22.13.1","_npmVersion":"10.9.2","dist":{"integrity":"sha512-1/22obknhoj56PcE+pZPp6AbWDdY55M81/ofpPW3Ltlp9Eh4zoFFLswvZmNpRTb790CY5tsNfgbYeNOqIARJfQ==","shasum":"91670e498ee3ff5739072ecabb5e17b6e564c01a","tarball":"http://registry-npm.ywwl.com/birpc/download/birpc-2.2.0.tgz","fileCount":8,"unpackedSize":32071,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIDExkZbnR+UQwYLuuzKChZSUbeR6JSlUYKqdu6JBd9DsAiBfml79xNe5PgxeBx/kqdpy2yQtch4GKnSnXDYNrYEP5Q=="}],"size":5859,"noattachment":false,"key":"/birpc/-/birpc-2.2.0.tgz"},"_npmUser":{"name":"antfu","email":"anthonyfu117@hotmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/birpc_2.2.0_1738671557366_0.2506241349813878"},"_hasShrinkwrap":false,"readme":"# birpc\n\n[![NPM version](https://img.shields.io/npm/v/birpc?color=a1b858&label=)](https://www.npmjs.com/package/birpc)\n\nMessage-based two-way remote procedure call. Useful for WebSockets and Workers communication.\n\n## Features\n\n- Intuitive - call remote functions just like locals, with Promise to get the response\n- TypeScript - safe function calls for arguments and returns\n- Protocol agonostic - WebSocket, MessageChannel, any protocols with messages communication would work!\n- Zero deps, ~0.5KB\n\n## Examples\n\n### Using WebSocket\n\nWhen using WebSocket, you need to pass your custom serializer and deserializer.\n\n#### Client\n\n```ts\nimport type { ServerFunctions } from './types'\n\nconst ws = new WebSocket('ws://url')\n\nconst clientFunctions: ClientFunctions = {\n  hey(name: string) {\n    return `Hey ${name} from client`\n  }\n}\n\nconst rpc = createBirpc<ServerFunctions>(\n  clientFunctions,\n  {\n    post: data => ws.send(data),\n    on: data => ws.on('message', data),\n    // these are required when using WebSocket\n    serialize: v => JSON.stringify(v),\n    deserialize: v => JSON.parse(v),\n  },\n)\n\nawait rpc.hi('Client') // Hi Client from server\n```\n\n#### Server\n\n```ts\nimport type { ClientFunctions } from './types'\nimport { WebSocketServer } from 'ws'\n\nconst serverFunctions: ServerFunctions = {\n  hi(name: string) {\n    return `Hi ${name} from server`\n  }\n}\n\nconst wss = new WebSocketServer()\n\nwss.on('connection', (ws) => {\n  const rpc = createBirpc<ClientFunctions>(\n    serverFunctions,\n    {\n      post: data => ws.send(data),\n      on: fn => ws.on('message', fn),\n      serialize: v => JSON.stringify(v),\n      deserialize: v => JSON.parse(v),\n    },\n  )\n\n  await rpc.hey('Server') // Hey Server from client\n})\n```\n\n### Circular References\n\nAs `JSON.stringify` does not supporting circular references, we recommend using [`structured-clone-es`](https://github.com/antfu/structured-clone-es) as the serializer when you expect to have circular references.\n\n```ts\nimport { parse, stringify } from 'structured-clone-es'\n\nconst rpc = createBirpc<ServerFunctions>(\n  functions,\n  {\n    post: data => ws.send(data),\n    on: fn => ws.on('message', fn),\n    // use structured-clone-es as serializer\n    serialize: v => stringify(v),\n    deserialize: v => parse(v),\n  },\n)\n```\n\n### Using MessageChannel\n\n[MessageChannel](https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel) will automatically serialize the message and support circular references out-of-box.\n\n```ts\nexport const channel = new MessageChannel()\n```\n\n#### Bob\n\n``` ts\nimport type { AliceFunctions } from './types'\nimport { channel } from './channel'\n\nconst Bob: BobFunctions = {\n  hey(name: string) {\n    return `Hey ${name}, I am Bob`\n  }\n}\n\nconst rpc = createBirpc<AliceFunctions>(\n  Bob,\n  {\n    post: data => channel.port1.postMessage(data),\n    on: fn => channel.port1.on('message', fn),\n  },\n)\n\nawait rpc.hi('Alice') // Hi Bob, I am Alice\n```\n\n#### Alice\n\n``` ts\nimport type { BobFunctions } from './types'\nimport { channel } from './channel'\n\nconst Alice: AliceFunctions = {\n  hi(name: string) {\n    return `Hi ${name}, I am Alice`\n  }\n}\n\nconst rpc = createBirpc<BobFunctions>(\n  Alice,\n  {\n    post: data => channel.port2.postMessage(data),\n    on: fn => channel.port2.on('message', fn),\n  },\n)\n\nawait rpc.hey('Alice') // Hey Alice, I am Bob\n```\n\n### One-to-multiple Communication\n\nRefer to [./test/group.test.ts](./test/group.test.ts) as an example.\n\n## Sponsors\n\n<p align=\"center\">\n  <a href=\"https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg\">\n    <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>\n  </a>\n</p>\n\n## License\n\n[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)\n","publish_time":1738671557570,"maintainers":[{"name":"antfu","email":"anthonyfu117@hotmail.com"},{"name":"oreanno","email":"foxzdavinci@gmail.com"}],"_cnpm_publish_time":1738671557570,"dist-tags":{"latest":"4.2.0"}}