Skip to content

Commit

Permalink
use trasient connections for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
d-roak committed Jul 1, 2024
1 parent e410794 commit 17195df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/canvas/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ICanvas } from "./objects/canvas";
// TODO: this should be superseded by wasm and main ts-topology library
export const handleCanvasMessages = (canvas: ICanvas, e: any) => {
if (e.detail.msg.topic === "_peer-discovery._p2p._pubsub") return;
if (e.detail.msg.topic === "topology::object::announcements") return;
const input = uint8ArrayToString(e.detail.msg.data);
const message = JSON.parse(input);
switch (message["type"]) {
Expand Down
1 change: 1 addition & 0 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@libp2p/autonat": "^1.0.0",
"@libp2p/bootstrap": "^10.1.0",
"@libp2p/circuit-relay-v2": "^1.0.24",
"@libp2p/dcutr": "^1.1.0",
"@libp2p/identify": "^2.0.2",
"@libp2p/interface-pubsub": "^4.0.1",
"@libp2p/pubsub-peer-discovery": "^10.0.2",
Expand Down
15 changes: 11 additions & 4 deletions packages/network/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class TopologyNetworkNode {
},
peerDiscovery: [
pubsubPeerDiscovery({
interval: 5_000,
interval: 10_000,
// topics: ["topology::discovery"],
}),
bootstrap({
list: [
Expand All @@ -69,7 +70,7 @@ export class TopologyNetworkNode {
streamMuxers: [yamux()],
transports: [
circuitRelayTransport({
discoverRelays: 1,
discoverRelays: 0,
}),
webRTC({
rtcConfiguration: {
Expand Down Expand Up @@ -105,6 +106,8 @@ export class TopologyNetworkNode {
multiaddr(`/p2p/${message.detail.propagationSource.toString()}`),
]);
});
this._node.addEventListener("peer:connect", (e) => console.log(e));
this._node.addEventListener("connection:open", (e) => console.log(e));

console.log(
"topology::network::start: Successfuly started topology network w/ peer_id",
Expand Down Expand Up @@ -196,7 +199,9 @@ export class TopologyNetworkNode {
async sendMessage(peerId: string, protocols: string[], message: string) {
try {
const connection = await this._node?.dial([multiaddr(`/p2p/${peerId}`)]);
const stream = <Stream>await connection?.newStream(protocols);
const stream = <Stream>await connection?.newStream(protocols, {
runOnTransientConnection: true,
});
stringToStream(stream, message);

console.log(
Expand All @@ -218,7 +223,9 @@ export class TopologyNetworkNode {
const peerId = peers[Math.floor(Math.random() * peers.length)];

const connection = await this._node?.dial(peerId);
const stream: Stream = (await connection?.newStream(protocols)) as Stream;
const stream: Stream = (await connection?.newStream(protocols, {
runOnTransientConnection: true,
})) as Stream;
stringToStream(stream, message);

console.log(
Expand Down
11 changes: 7 additions & 4 deletions packages/network/src/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ export const createRelayNode = async () => {
listen: ["/ip4/0.0.0.0/tcp/50000/ws", "/ip4/0.0.0.0/tcp/50001"],
},
connectionEncryption: [noise()],
peerDiscovery: [pubsubPeerDiscovery()],
peerDiscovery: [
pubsubPeerDiscovery({
interval: 10_000,
// topics: ["topology::discovery"],
}),
],
services: {
autonat: autoNAT(),
identify: identify(),
pubsub: gossipsub({
runOnTransientConnection: true,
}),
pubsub: gossipsub(),
relay: circuitRelayServer(),
},
streamMuxers: [yamux()],
Expand Down

0 comments on commit 17195df

Please sign in to comment.