nostr-protocol / nips

Nostr Implementation Possibilities
2.3k stars 563 forks source link

WebRTC signaling for peer-to-peer connection establishment #771

Open stefanha opened 1 year ago

stefanha commented 1 year ago

I experimented with using nostr encrypted direct messages (NIP-04) for WebRTC signaling. This allows web, mobile, or desktop applications to establish peer-to-peer connections using just a nostr relay and a pubkey.

I'm not proposing a NIP but wanted to record what I found when building a prototype 2-player tic-tac-toe game (HTML/JS) that uses nostr to establish the WebRTC connection. There is no server involved except for a public nostr relay and a public STUN server - the game is peer-to-peer.

The main issues I found were:

Other than that, it's amazing that WebRTC connections can be established with nostr's help. I think this could enable all sorts of peer-to-peer applications - both using the nostr protocol in a p2p fashion and other protocols.

You can find the code here: https://gitlab.com/stefanha/tic-tac-toe-p2p/

vitorpamplona commented 1 year ago

Just use a different event kind with the same structure. Otherwise, you are going to destroy people's DMs with unrenderable machine code in all clients.

Literally, just pick a different number for the game. There is no need to make a generic WebRtc event type. Many NIPs are already RPC-like (NIP-46, NIP-47, etc), so just pick a new number and done.

stefanha commented 1 year ago

Thank you, @vitorpamplona! relayable.org's strfry relay doesn't accept the event when an ephemeral kind is used due to a strfry bug (fixed by commit https://github.com/hoytech/strfry/commit/6d8aeaa8814452682a3060d6bfa0a42ba5a6029a). I think that's why I couldn't get ephemeral events working.

By the way, although I used WebRTC for data, I guess nostr clients could use this to add video/audio calls relatively easily.

vitorpamplona commented 1 year ago

Thank you, @vitorpamplona! relayable.org's strfry relay doesn't accept the event when an ephemeral kind is used due to a strfry bug (fixed by commit https://github.com/hoytech/strfry/commit/6d8aeaa8814452682a3060d6bfa0a42ba5a6029a). I think that's why I couldn't get ephemeral events working.

Yep, every time you create a new event kind, you have to talk to relay operators of your choice to enable it.

By the way, although I used WebRTC for data, I guess nostr clients could use this to add video/audio calls relatively easily

In theory yes, In practice I doubt it. The event would be too big to make sense.

stefanha commented 1 year ago

By the way, although I used WebRTC for data, I guess nostr clients could use this to add video/audio calls relatively easily

In theory yes, In practice I doubt it. The event would be too big to make sense.

Only a few small events need to be exchanged in order to set up a WebRTC peer-to-peer connection for audio/video conferencing. The audio/video data is sent over the peer-to-peer connection, not over nostr.

Relays should have no issues transferring these WebRTC signaling events. The Matrix chat protocol uses a similar approach for audio/video calling.

vitorpamplona commented 1 year ago

Relays should have no issues transferring these WebRTC signaling events. The Matrix chat protocol uses a similar approach for audio/video calling.

Let's do it, then! :)

mekefly commented 11 months ago

That's a very interesting idea. I support you. Maybe you can do more in the future, such as video calls.