{"name":"birpc","version":"0.0.2","description":"Message based Two-way remote procedure call","keywords":["rpc","messages"],"homepage":"https://github.com/antfu/birpc#readme","bugs":{"url":"https://github.com/antfu/birpc/issues"},"repository":{"type":"git","url":"git+https://github.com/antfu/birpc.git"},"funding":{"url":"https://github.com/sponsors/antfu"},"license":"MIT","author":"Anthony Fu <anthonyfu117@hotmail.com>","sideEffects":false,"exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.mjs","types":"./dist/index.d.ts"}},"main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","devDependencies":{"@antfu/eslint-config":"^0.14.0","@antfu/ni":"^0.12.0","@types/node":"^17.0.0","bumpp":"^7.1.1","eslint":"^8.5.0","esno":"^0.13.0","typescript":"^4.5.4","unbuild":"^0.6.7","vitest":"^0.0.101","vite":"^2.7.3"},"scripts":{"build":"unbuild","dev":"unbuild --stub","lint":"eslint --ext .js,.ts .","release":"bumpp --commit --push --tag && pnpm publish","start":"esno src/index.ts","test":"vitest"},"_id":"birpc@0.0.2","_integrity":"sha512-D5D88rjfiXpaGJ9j0MWZrihp1NsBER1v5lyXp6/W23bQIvwWCH8z+Pk1XSnwkIqSnD4s2EjLJq5VmW5u6X9Cgg==","_resolved":"/Users/antfu/i/birpc/birpc-0.0.2.tgz","_from":"file:birpc-0.0.2.tgz","_nodeVersion":"16.13.0","_npmVersion":"8.1.0","dist":{"integrity":"sha512-D5D88rjfiXpaGJ9j0MWZrihp1NsBER1v5lyXp6/W23bQIvwWCH8z+Pk1XSnwkIqSnD4s2EjLJq5VmW5u6X9Cgg==","shasum":"9eaae135769b44c74b0911da1de40078de20f981","tarball":"http://registry-npm.ywwl.com/birpc/download/birpc-0.0.2.tgz","fileCount":6,"unpackedSize":7812,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhyHVaCRA9TVsSAnZWagAAlwIQAI61CudK9TZjvQz12ATy\njClCnu3bFRuMe0Hu9ZtMn1z1afN9DA+j3NRVPPxCSXFTv48RYRP2jq6cEUVB\nxnfBI3tVgfQgPs1rdP3KqUbxdlb7YTo6m1UlY/e+aYx+8w1ZfK3UkJPw3gB9\nky/x76Q/ZBybqu1t/C1wVm3KuG0DPT/fWib5ArTBcmKatPQBbtSzfK/cp+0z\nru334kQyK3XOJ9ohZ3pHNJjvgwGiFeJz2QNG/34hucnezfKQXqIvH/3eaTvE\nC2ZZr+dcw7iht4f34H58La74cAZwQikAZzf1B80khF+71kKaUtsPsMsycmTL\no1KDCI4wVjLwc6WbLF8E2pDyFsFGTkUYCSw+olrZmSFpTvjajp3XPtmykDVM\nd1iXGuTI+MKitUj5eo5lYle7wst1OegNu5QUpzYkvGL0At2cIt8jwzwfwqAk\nuMjcfBnWQln6lWD+bw87vLgcGfqqg+DrDWlSW7Idqvd1vonyQbn669Vg57Db\nZdDszisf5xrqdPl/jYmzeJjZhgZnariPd0c0PvOAIWX74CxtRxG9Rv+SLhpG\nbKrj0sS2kwIklsbZVG9YO/onY7vsFS4BEK6uNmE/Co+s5iJ6nu4E62jOTGs/\nWAs+OzsiVz7P03Tsem5XbIo8TnM9Rd5JhcrQZns6hw7hG7om995r52YtlwVG\n/n0G\r\n=0M19\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDyTD8kF138nz88F/O5EYZqlk5VnWGC8P89NsvVPEZr6AIgDFwHaLYOdzxJVcPnH0/NDrjA0457uv/28wmshgqkCFo="}],"size":2941,"noattachment":false,"key":"/birpc/-/birpc-0.0.2.tgz"},"_npmUser":{"name":"antfu","email":"anthonyfu117@hotmail.com"},"directories":{},"maintainers":[{"name":"antfu","email":"anthonyfu117@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/birpc_0.0.2_1640527193960_0.9039054555612949"},"_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 { WebSocketServer } from 'ws'\nimport type { ClientFunctions } from './types'\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: data => ws.on('message', data),\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 [`flatted`](https://github.com/WebReflection/flatted) as the serializer when you expect to have circular references.\n\n```ts\nimport { parse, stringify } from 'flatted'\n\nconst rpc = createBirpc<ServerFunctions>(\n  functions,\n  {\n    post: data => ws.send(data),\n    on: data => ws.on('message', data),\n    // use flatted 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: data => channel.port1.on('message', data),\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: data => channel.port2.on('message', data),\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":1640527194295,"_cnpm_publish_time":1640527194295,"dist-tags":{"latest":"4.2.0"}}