Implement WebSocket Transport with IPv6 Support and Security Integration #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a complete WebSocket transport for py-libp2p, enabling interoperability with js-libp2p and go-libp2p implementations that use
/ws
multiaddrs. The implementation includes comprehensive IPv6 support, security protocol integration, and automatic transport selection.Key Features
WebSocket Transport Implementation
/ws
protocol support with proper multiaddr parsing and handling::1
→ws://[::1]:8080/
)Security Integration
The WebSocket transport seamlessly integrates with py-libp2p's existing security protocols:
Automatic Transport Selection
Implements a transport registry system that automatically selects the correct transport based on multiaddr protocols:
IPv6 and Cross-Platform Support
Addresses the IPv6 support gap identified in the original PR discussion:
Implementation Details
Core Components
WebsocketTransport
- Main transport class implementing dial and listener creationWebsocketListener
- Handles incoming WebSocket connections with proper address bindingP2PWebSocketConnection
- Wraps WebSocket connections to provide the raw stream interface required by libp2p protocolsTransportRegistry
- Dynamic transport selection system based on multiaddr protocolsError Handling
trio-websocket
dependenciesTesting and Verification
Includes comprehensive test coverage:
Usage Examples
Basic WebSocket Server
Cross-Implementation Interoperability
The implementation is fully compatible with js-libp2p:
Backward Compatibility
The implementation maintains full backward compatibility:
Dependencies
Adds
trio-websocket>=0.11.0
as a new dependency for WebSocket functionality. The transport gracefully handles missing dependencies and provides clear error messages when WebSocket features are attempted without the required packages.Status Update
Updates the README.md transport status from ❌ to ✅ for
libp2p-websocket
, reflecting the completed implementation.This implementation provides a solid foundation for WebSocket connectivity in py-libp2p while maintaining the high standards of security and interoperability expected from the libp2p ecosystem.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.