libp2p-transport
v0.4.2
Published
peer to peer rpc/channel communication based on libp2p
Downloads
4
Readme
libp2p-transport
peer to peer rpc/channel communication based on libp2p
Features
- ⚡Efficient transmission
- 🤝Bidirectional channel support
- 🎡AsyncIterator style
- 🪐Peer to peer connect
- 🔢Data codec agnostic(json, protobuff, etc.)
- 📡Transport protocol agnostic(tcp/udp, ws, webtransport, etc.)
- 💪Cross-peer error capture
Quickstart
first
import { client, server } from "libp2p-transport";
// server side
const { handle, serve } = server(libp2p);
// client side
const conn = await client(libp2p, addr);
↓↓↓ USAGE ↓↓↓
- [ ] One request one response
// server side
await handle<number, number>(
"add",
async ({ data, send, done }) => {
await send(data + 1);
await done();
},
);
// client side
const add = await conn<number, number>("add");
await add.send(1);
(await add.next()).value; // 2
// or
await add(1); // [2]
- [ ] One request multi response
// server side
await handle<number, number>(
"adding",
async ({ data, send, done }) => {
await send(data + 1);
await send(data + 2);
await send(data + 3);
await done();
},
);
// client side
const adding = await conn<number, number>("adding");
await adding.send(1);
for await (const msg of adding) {
console.log(msg); // 2, 3, 4
}
// or
await adding(1); // [2, 3, 4]
- [ ] Bidirectional channel(infinite request infinite response)
// server side
const service = () => {
const handle = ({ data, send }) => send(data + 1);
return handle;
};
await serve<number, number>(
"channelAdd",
service,
);
// client side
const channelAdd = await conn<number, number>("channelAdd");
let num = 0;
await channelAdd.send(num);
for await (const msg of channelAdd) {
msg === num; // true
num++;
await channel.send(num);
}
more examples in test