{"name":"birpc","version":"2.8.0","keywords":["rpc","messages"],"author":{"name":"Anthony Fu","email":"anthonyfu117@hotmail.com"},"license":"MIT","_id":"birpc@2.8.0","maintainers":[{"name":"antfu","email":"anthonyfu117@hotmail.com"},{"name":"oreanno","email":"foxzdavinci@gmail.com"}],"homepage":"https://github.com/antfu-collective/birpc#readme","bugs":{"url":"https://github.com/antfu-collective/birpc/issues"},"dist":{"shasum":"064c90bda7912ef8aebd544f174ae1c9bc230c71","tarball":"http://registry-npm.ywwl.com/birpc/download/birpc-2.8.0.tgz","fileCount":8,"integrity":"sha512-Bz2a4qD/5GRhiHSwj30c/8kC8QGj12nNDwz3D4ErQ4Xhy35dsSDvF+RA/tWpjyU0pdGtSDiEk6B5fBGE1qNVhw==","signatures":[{"sig":"MEQCICjjMS/NrpemLflC9qYfKiRwqTeSgihzJMehnn3KlZ2OAiAmTAR+2wWZ9Vh3YY1zLMcCgsJuSh8AZHsYamoC/uIJWA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/birpc@2.8.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":47416,"size":7185,"noattachment":false,"key":"/birpc/-/birpc-2.8.0.tgz"},"main":"./dist/index.cjs","type":"module","_from":"file:birpc-2.8.0.tgz","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"funding":"https://github.com/sponsors/antfu","scripts":{"dev":"unbuild --stub","lint":"eslint .","test":"vitest","build":"unbuild","start":"tsx src/index.ts","release":"bumpp","typecheck":"tsc --noEmit"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:28daa56c-d12b-4800-a012-87479190c889"}},"_resolved":"/tmp/7339933ca6e06bb62543ec51bfcedac8/birpc-2.8.0.tgz","_integrity":"sha512-Bz2a4qD/5GRhiHSwj30c/8kC8QGj12nNDwz3D4ErQ4Xhy35dsSDvF+RA/tWpjyU0pdGtSDiEk6B5fBGE1qNVhw==","repository":{"url":"git+https://github.com/antfu-collective/birpc.git","type":"git"},"_npmVersion":"11.6.1","description":"Message based Two-way remote procedure call","directories":{},"sideEffects":false,"_nodeVersion":"24.11.0","_hasShrinkwrap":false,"devDependencies":{"tsx":"^4.19.3","vite":"^6.2.3","bumpp":"^10.1.0","eslint":"^9.23.0","vitest":"^3.0.9","unbuild":"^3.5.0","@antfu/ni":"^24.3.0","typescript":"^5.8.2","@types/node":"^22.13.13","@vitest/coverage-v8":"3.0.9","@antfu/eslint-config":"^4.11.0"},"_npmOperationalInternal":{"tmp":"tmp/birpc_2.8.0_1762672519184_0.8457684294006149","host":"s3://npm-registry-packages-npm-production"},"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: fn => ws.on('message', fn),\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### 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('Bob') // 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### Using SSE + HTTP POST\n\nbirpc can also run over **Server-Sent Events** (server → client) paired with\n**HTTP POST** (client → server), so a browser and an HTTP server can call each\nother with the exact same DX as the WebSocket example above. Because SSE is only\nhalf of a duplex channel, the `post`/`on` pairing takes a bit more code than a\nWebSocket - it is **not** part of the package, but a copy-paste recipe you own:\n[`examples/sse/channel`](./examples/sse/channel) (`shared.ts` + `client.ts` +\n`server.ts`, dependency-free). Copy that folder into your project and adjust it.\n\n#### Client\n\n```ts\nimport type { ServerFunctions } from './types'\nimport { createBirpc } from 'birpc'\n// copied from examples/sse/channel\nimport { createSseClientChannel } from './channel/client'\n\nconst clientFunctions: ClientFunctions = {\n  hey(name: string) {\n    return `Hey ${name} from client`\n  },\n}\n\nconst channel = createSseClientChannel('http://localhost:3737')\n\nconst rpc = createBirpc<ServerFunctions>(clientFunctions, {\n  post: channel.post,\n  on: channel.on,\n  serialize: v => JSON.stringify(v),\n  deserialize: v => JSON.parse(v),\n})\n\nawait rpc.hi('Client') // Hi Client from server\n```\n\n#### Server\n\n```ts\nimport type { ClientFunctions } from './types'\nimport { createServer } from 'node:http'\nimport { createBirpc } from 'birpc'\n// copied from examples/sse/channel\nimport { createSseSessionManager } from './channel/server'\n\nconst serverFunctions: ServerFunctions = {\n  hi(name: string) {\n    return `Hi ${name} from server`\n  },\n}\n\nconst sessions = createSseSessionManager()\n\ncreateServer(async (req, res) => {\n  if (req.method === 'GET' && req.url === '/sse') {\n    const { channel } = sessions.open(req, res)\n    const rpc = createBirpc<ClientFunctions>(serverFunctions, {\n      post: channel.post,\n      on: channel.on,\n      serialize: v => JSON.stringify(v),\n      deserialize: v => JSON.parse(v),\n    })\n    await rpc.hey('Server') // Hey Server from client\n    return\n  }\n  if (req.method === 'POST' && req.url === '/rpc')\n    await sessions.handlePost(req, res)\n}).listen(3737)\n```\n\nSee [`examples/sse`](./examples/sse) for the complete, runnable demo (Node client +\nserver and a browser page) plus a writeup of how the transport works.\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' alt=\"Sponsors\"/>\n  </a>\n</p>\n\n## License\n\n[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)\n","publish_time":1762672519374,"_cnpm_publish_time":1762672519374,"dist-tags":{"latest":"4.2.0"}}