Gathering detailed insights and metrics for birpc
Gathering detailed insights and metrics for birpc
Gathering detailed insights and metrics for birpc
Gathering detailed insights and metrics for birpc
npm install birpc
Typescript
Module System
Node Version
NPM Version
TypeScript (99.76%)
JavaScript (0.24%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
467 Stars
93 Commits
24 Forks
3 Watchers
2 Branches
8 Contributors
Updated on Jul 14, 2025
Latest Version
2.5.0
Package Id
birpc@2.5.0
Unpacked Size
33.87 kB
Size
6.01 kB
File Count
8
NPM Version
10.9.2
Node Version
22.14.0
Published on
Jul 14, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Message-based two-way remote procedure call. Useful for WebSockets and Workers communication.
When using WebSocket, you need to pass your custom serializer and deserializer.
1import type { ServerFunctions } from './types' 2 3const ws = new WebSocket('ws://url') 4 5const clientFunctions: ClientFunctions = { 6 hey(name: string) { 7 return `Hey ${name} from client` 8 } 9} 10 11const rpc = createBirpc<ServerFunctions>( 12 clientFunctions, 13 { 14 post: data => ws.send(data), 15 on: data => ws.on('message', data), 16 // these are required when using WebSocket 17 serialize: v => JSON.stringify(v), 18 deserialize: v => JSON.parse(v), 19 }, 20) 21 22await rpc.hi('Client') // Hi Client from server
1import type { ClientFunctions } from './types' 2import { WebSocketServer } from 'ws' 3 4const serverFunctions: ServerFunctions = { 5 hi(name: string) { 6 return `Hi ${name} from server` 7 } 8} 9 10const wss = new WebSocketServer() 11 12wss.on('connection', (ws) => { 13 const rpc = createBirpc<ClientFunctions>( 14 serverFunctions, 15 { 16 post: data => ws.send(data), 17 on: fn => ws.on('message', fn), 18 serialize: v => JSON.stringify(v), 19 deserialize: v => JSON.parse(v), 20 }, 21 ) 22 23 await rpc.hey('Server') // Hey Server from client 24})
As JSON.stringify
does not supporting circular references, we recommend using structured-clone-es
as the serializer when you expect to have circular references.
1import { parse, stringify } from 'structured-clone-es' 2 3const rpc = createBirpc<ServerFunctions>( 4 functions, 5 { 6 post: data => ws.send(data), 7 on: fn => ws.on('message', fn), 8 // use structured-clone-es as serializer 9 serialize: v => stringify(v), 10 deserialize: v => parse(v), 11 }, 12)
MessageChannel will automatically serialize the message and support circular references out-of-box.
1export const channel = new MessageChannel()
1import type { AliceFunctions } from './types' 2import { channel } from './channel' 3 4const Bob: BobFunctions = { 5 hey(name: string) { 6 return `Hey ${name}, I am Bob` 7 } 8} 9 10const rpc = createBirpc<AliceFunctions>( 11 Bob, 12 { 13 post: data => channel.port1.postMessage(data), 14 on: fn => channel.port1.on('message', fn), 15 }, 16) 17 18await rpc.hi('Bob') // Hi Bob, I am Alice
1import type { BobFunctions } from './types' 2import { channel } from './channel' 3 4const Alice: AliceFunctions = { 5 hi(name: string) { 6 return `Hi ${name}, I am Alice` 7 } 8} 9 10const rpc = createBirpc<BobFunctions>( 11 Alice, 12 { 13 post: data => channel.port2.postMessage(data), 14 on: fn => channel.port2.on('message', fn), 15 }, 16) 17 18await rpc.hey('Alice') // Hey Alice, I am Bob
Refer to ./test/group.test.ts as an example.
MIT License © 2021 Anthony Fu
No vulnerabilities found.
No security vulnerabilities found.