Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Node.js peer? #214

Open
achingbrain opened this issue Feb 12, 2025 · 6 comments
Open

Add a Node.js peer? #214

achingbrain opened this issue Feb 12, 2025 · 6 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@achingbrain
Copy link
Member

The only JS version here runs on a web page.

There should be a Node.js version that shows how to do the server-side version.

@achingbrain achingbrain added good first issue Good for newcomers help wanted Extra attention is needed labels Feb 12, 2025
@devlux76
Copy link

It's really straightforward but probably not in scope for this project because a lot of what this does, doesn't make any sense at all in a server only context.

For instance, the WebRTC stuff is a monkey patch on top of NodeJS and what are you going to stream from a server over WebRTC that wouldn't be better done as a relay?

The other issue is hosting. For a full-on server implementation, you're going to need a long-running server with a plethora of ports exposed. Most hosting environments will intentionally limit you to what ports you can expose and manually punching that many holes in their firewall is likely to get you denied.

What you could do is run a circuit relay in node. The project could really use more of those they're the backbone of the p2p network.
A simple circuit relay looks like this...

import {createLibp2p} from 'libp2p';
import {circuitRelayTransport} from '@libp2p/circuit-relay-v2';

const node = await createLibp2p({
transports: [
     circuitRelayTransport()
]
});

That's it! That's all there is to it,

@achingbrain
Copy link
Member Author

I'm not sure I agree.

The point here is to show that Node.js can make and accept server-style connections to go and rust peers via TCP, initiate direct connections to browsers via WebRTC (go and rust cannot do this at the time of writing, they have to dial a browser via Circuit Relay, then hope it dials them back via the unilateral connection upgrade part of DCUtR), but also accept connections from browsers via WebRTC-Direct and WebSockets.

Since this project is the starting point for a lot of people, it makes sense to show what connectivity is available in which environments and Node.js is missing here currently.

what are you going to stream from a server over WebRTC that wouldn't be better done as a relay?

Circuit Relay v2 streams are strictly time and data limited - once you hit those limits the server will close the connection abruptly so they're not really suited for application use. Instead they should be used to obtain metadata about a peer via Identify and/or to open a direct connection.

manually punching that many holes in their firewall is likely to get you denied

I don't follow the logic here. If you run a TCP listener on a platform without SO_REUSEPORT enabled, all opened connections get shifted to an ephemeral port with no manual configuration required. This doesn't generally result in applications getting kicked off hosting platforms.

That said, the WebRTC-Direct transport multiplexes a single UDP port to allow running multiple connections concurrently. If this is actually a problem, I'm sure we can enable the regular WebRTC transport to use port muxing too in order to solve it.

A simple circuit relay looks like this...

The suggested configuration creates a that's only capable of dialling an existing relay, and even then it's missing the additional transport (and maybe muxer/encrypter) needed to run the Circuit Relay protocol over.

See Set up a relay node from the js-libp2p Circuit Relay example for more.

@Nkovaturient
Copy link

Nkovaturient commented Feb 19, 2025

Hello @achingbrain ,

As gist of the required implementations, Node.js peer should demonstrate:

  • TCP connectivity with Go/Rust peers
  • WebRTC-Direct dialing to browsers
  • WebSocket listener for browser connections
  • DCUtR protocol support for hole punching

Shall I start with setting up relay node, listener and dial, chat streams as given here https://github.com/libp2p/js-libp2p-examples/tree/main/examples/js-libp2p-example-chat

I will study further and dive into the complexities.

@achingbrain
Copy link
Member Author

Thanks!

This should be "WebRTC, WebRTC Direct and WebSocket listeners for browser connections" and "WebRTC dialing to browsers" (not WebRTC Direct).

If you are not clear on the difference between WebRTC and WebRTC Direct in libp2p, the differences are described in the spec.

@Nkovaturient
Copy link

sure, thank you.
I will look into it and get back with an update.

@Nkovaturient
Copy link

Hey @achingbrain ,
I have endeavoured to achieve the desired nodejs-peer setup as of now. Kindly review and guide me further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants