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.67%)
JavaScript (0.33%)
Total Downloads
36,636,680
Last Day
73,081
Last Week
503,688
Last Month
3,779,861
Last Year
30,783,265
389 Stars
75 Commits
16 Forks
3 Watching
1 Branches
7 Contributors
Latest Version
0.2.19
Package Id
birpc@0.2.19
Unpacked Size
27.69 kB
Size
5.43 kB
File Count
8
NPM Version
10.8.2
Node Version
20.18.0
Publised On
08 Oct 2024
Cumulative downloads
Total Downloads
Last day
-25.4%
73,081
Compared to previous day
Last week
-40.2%
503,688
Compared to previous week
Last month
-4.3%
3,779,861
Compared to previous month
Last year
445.8%
30,783,265
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 flatted
as the serializer when you expect to have circular references.
1import { parse, stringify } from 'flatted' 2 3const rpc = createBirpc<ServerFunctions>( 4 functions, 5 { 6 post: data => ws.send(data), 7 on: fn => ws.on('message', fn), 8 // use flatted 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('Alice') // 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.