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

feat: direct messages #175

Merged
merged 13 commits into from
Sep 17, 2024
Merged

feat: direct messages #175

merged 13 commits into from
Sep 17, 2024

Conversation

dozyio
Copy link
Contributor

@dozyio dozyio commented Jul 17, 2024

  • Adds direct messaging between connected peers with protobufs

Hopefully a bit simpler than the previous PR

Copy link

vercel bot commented Jul 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
universal-connectivity ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 5, 2024 8:59pm

js-peer/src/context/ctx.tsx Outdated Show resolved Hide resolved
stream?.abort(e)
throw e
} finally {
await stream?.close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what the tradeoffs of opening and closing a stream for every message are. Not a blocker, but useful for learning.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re incurring a small amount of overhead - there will be a round trip for opening a stream, two for multistream select (e.g. negotiating a protocol) and then one for closing the stream afterwards.

This may not be disastrous depending on the frequency of message sending and your network latency to the remote peer, it’s application-specific.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another factor is that opening/closing streams can be useful if the remote has a strict limit on the number of incoming protocol streams.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re incurring a small amount of overhead - there will be a round trip for opening a stream, two for multistream select (e.g. negotiating a protocol) and then one for closing the stream afterwards.

If the connection is open already, and const stream = await conn.newStream(DIRECT_MESSAGE_PROTOCOL, { negotiateFully: false }) is called (line 106), will it entail two roundtrips for multistream select?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah - negotiateFully: false will just send the mss header, the line break, and the protocol name in one go, assume success, and carry on so yeah, it doesn't wait to read anything. It'll still wait for the message to be passed to the network layer but that's pretty fast.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought on this was that opening/closing a stream would have a relatively low overhead compared to opening a connection - it also simplifies the code a bit by having one less thing to track / cleanup.

I'm not sure how the connection manager handles connections but would assume that it wouldn't close connections with an open stream, thus best to close the stream when possible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would assume that it wouldn't close connections with an open stream

I believe that's correct

thus best to close the stream when possible.

mm, not sure I understood what you mean. Do you mean that you rely on another open stream, i.e.the gossipsub stream that remains open to the peer for the public chat?

Copy link
Contributor Author

@dozyio dozyio Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would assume that it wouldn't close connections with an open stream

I believe that's correct

thus best to close the stream when possible.

mm, not sure I understood what you mean. Do you mean that you rely on another open stream, i.e.the gossipsub stream that remains open to the peer for the public chat?

I mean so that it frees the stream resource, so that the connection manager can choose to close the DM connection if needed. Guess I should look at the connection manager code, might be assuming too much here.

@2color
Copy link
Collaborator

2color commented Jul 24, 2024

I made a couple of small suggestions in as I'm going through the PR and testing dozyio#6 (which I couldn't push directly here)

leaveTo="transform opacity-0 scale-95"
>
<MenuItems className="absolute left-0 z-10 mt-2 w-48 origin-top-right rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none">
{!identified &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How were you able to test this?

Copy link
Contributor Author

@dozyio dozyio Jul 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How were you able to test this?

I was having an issues connect to the Go relay node (hit a limit on relay connections) and was only able to connect to the rust peer. Gossiping still worked so could see the messages and could test unidentified peers that way, although there is a slight issue.

A peer doesn't add its peer record to the DHT when only using delegated routing so there isn't a way to resolve the multiaddr from the peer id (this was when the rust peer didn't have gossip peer discovery). I haven't thought of a way around this as yet / maybe a non-issue.

Copy link
Collaborator

@2color 2color left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! I left a bunch of comments and small suggestions in a separate PR.

From a UX perspective, my suggestion would be to remove the peer menu to reduce the number of clicks. For peer not supporting DMs, this could be a hover tooltip.

Thanks @dozyio

* chore: small simplification

* chore: remove from field which can be derived

---------

Co-authored-by: Daniel N <2color@users.noreply.github.com>
@dozyio
Copy link
Contributor Author

dozyio commented Jul 25, 2024

Great stuff! I left a bunch of comments and small suggestions in a separate PR.

From a UX perspective, my suggestion would be to remove the peer menu to reduce the number of clicks. For peer not supporting DMs, this could be a hover tooltip.

Thanks @dozyio

Thanks for the PR - merged... was going to add a bit more functionality with the menu, but can do that on a separate pr. Will remove for now

@dozyio dozyio marked this pull request as draft July 25, 2024 22:57
@dozyio dozyio marked this pull request as ready for review July 26, 2024 00:46
@2color
Copy link
Collaborator

2color commented Aug 13, 2024

@dozyio Is this ready for another review?

@dozyio
Copy link
Contributor Author

dozyio commented Aug 29, 2024

@dozyio Is this ready for another review?

@2color Sorry didn't see this reply! - yes good for another review

Copy link
Collaborator

@2color 2color left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Just need to resolve the conflicts.

I would highlight the peer you are DMing in the peer list. But we can do that in a separate PR.

@dozyio
Copy link
Contributor Author

dozyio commented Sep 5, 2024

Just need to resolve the conflicts.

resolved

I would highlight the peer you are DMing in the peer list. But we can do that in a separate PR.

Good idea - will sort something out next week

@dozyio
Copy link
Contributor Author

dozyio commented Sep 13, 2024

@2color can we merge this?

would like to do the js-libp2p 2.0 upgrade next week if possible

@2color
Copy link
Collaborator

2color commented Sep 13, 2024

Let's go!

@2color 2color merged commit 750150c into libp2p:main Sep 17, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants