RangerMauve / hypercore-fetch

Implementation of Fetch that uses the Hyper SDK for loading p2p content
MIT License
37 stars 13 forks source link

Think of API for extension messages / peers #5

Closed RangerMauve closed 3 years ago

RangerMauve commented 4 years ago

We need an API for listing peer metadata as well as sending extension messages to peers.

I propose something like

PEERS / -> [{id, remotePublicKey, remoteAddress}]

EXTENSION-REGISTER /{extension name in the path} -> text/event-stream

EXTENSION-SEND /{extension name in the path}?peer={optional peer id} {extension data in body -> 200 OK

The PEER method will list the metadata about the peers you're connected with. Maybe there should be something here for filtering peers by which ones have a certain extension? Maybe we could list which extensions peers are registered on too?

The EXTENSION-REGISTER will register an extension message handler if one doesn't exist. It will then start an event source stream and send new events down the wire. This should be used with the EventSource API in browsers. We need to figure out a way to specify which peer an event has come from. Maybe in the message name? Maybe in the lastMessageId?

Lastly, EXTENSION-SEND will send out an extension message. The body will be used as the extension message contents. You can optionally specify a peer's id in the querystring to send to a specific peer (or multiple specific peers), or leave the querystring blank to broadcast to everyone. This is similar to a POST.

How does that sound? I'm mostly iffy about the peer info.

cc @DeltaF1 @calm-rad

DeltaF1 commented 4 years ago

Do extension messages work similarly for most protocols?

RangerMauve commented 4 years ago

I don't know if extension messages are a feature of all protocols.

I know that BitTorrent has then and I think they're sent at the torrent-replication layer. hypercore-protocol actually has two layers of extension messages, one at the top level of the protocol, and one that's per hypercore replication channel that's multiplexed over the connection.

I was thinking this's be the hypercore channel extension messages to start.

I don't think IPFS has extension messages because they expect you to use libp2p directly.

Similarly, I don't think EarthStar has extension messages since it's replication seems to be based on HTTP requests.

RangerMauve commented 3 years ago

This has been implemented as of #25 😁

RangerMauve commented 3 years ago

Ended up using a special folder called /$/extensions/ to do Extension stuff. You can subscribe to a text/event-stream of all extension messages, or POST to /$/extensions/NAME_HERE to broadcast data out to all peers.