|
| 1 | +WebSocket Transport Examples |
| 2 | +============================ |
| 3 | + |
| 4 | +This guide demonstrates how to use the WebSocket transport in py-libp2p, including WS (WebSocket) and WSS (WebSocket Secure) protocols, SOCKS proxy support, AutoTLS, and advanced configuration options. |
| 5 | + |
| 6 | +Quick Start |
| 7 | +----------- |
| 8 | + |
| 9 | +Basic WebSocket transport setup: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from libp2p import new_host |
| 14 | + from libp2p.transport.websocket import WebsocketTransport, WebsocketConfig |
| 15 | + from libp2p.transport.upgrader import TransportUpgrader |
| 16 | + from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport |
| 17 | + from libp2p.stream_muxer.yamux.yamux import Yamux |
| 18 | + from libp2p.custom_types import TProtocol |
| 19 | + from libp2p.crypto.rsa import create_new_key_pair |
| 20 | + from multiaddr import Multiaddr |
| 21 | +
|
| 22 | + # Generate a key pair |
| 23 | + key_pair = create_new_key_pair() |
| 24 | +
|
| 25 | + # Create upgrader with security and muxer |
| 26 | + upgrader = TransportUpgrader( |
| 27 | + secure_transports_by_protocol={ |
| 28 | + TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair) |
| 29 | + }, |
| 30 | + muxer_transports_by_protocol={TProtocol("/yamux/1.0.0"): Yamux} |
| 31 | + ) |
| 32 | +
|
| 33 | + # Create WebSocket transport |
| 34 | + config = WebsocketConfig() |
| 35 | + transport = WebsocketTransport(upgrader, config=config) |
| 36 | +
|
| 37 | + # Create host with WebSocket transport |
| 38 | + # Note: new_host handles transport creation internally if not overridden, |
| 39 | + # but this shows how to set it up manually if needed. |
| 40 | + host = new_host( |
| 41 | + listen_addrs=[Multiaddr("/ip4/127.0.0.1/tcp/8080/ws")] |
| 42 | + ) |
| 43 | +
|
| 44 | +WS vs WSS |
| 45 | +--------- |
| 46 | + |
| 47 | +**WS (Insecure WebSocket):** |
| 48 | + |
| 49 | +Use for development and testing. No TLS encryption: |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + ws_addr = Multiaddr("/ip4/127.0.0.1/tcp/8080/ws") |
| 54 | + host = new_host(listen_addrs=[ws_addr]) |
| 55 | +
|
| 56 | +**WSS (Secure WebSocket):** |
| 57 | + |
| 58 | +Use for production. Requires TLS configuration: |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | +
|
| 62 | + import ssl |
| 63 | +
|
| 64 | + # Create TLS context |
| 65 | + tls_context = ssl.create_default_context() |
| 66 | + tls_context.check_hostname = False |
| 67 | + tls_context.verify_mode = ssl.CERT_NONE # For testing only |
| 68 | +
|
| 69 | + # Configure transport with TLS |
| 70 | + config = WebsocketConfig(tls_server_config=tls_context) |
| 71 | + transport = WebsocketTransport(upgrader, config=config) |
| 72 | +
|
| 73 | + wss_addr = Multiaddr("/ip4/127.0.0.1/tcp/8080/wss") |
| 74 | + host = new_host(listen_addrs=[wss_addr]) |
| 75 | +
|
| 76 | +SOCKS Proxy Configuration |
| 77 | +------------------------- |
| 78 | + |
| 79 | +**Using Proxy Factory Function:** |
| 80 | + |
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + from libp2p.transport.websocket import WithProxy |
| 84 | +
|
| 85 | + # Configure with SOCKS5 proxy |
| 86 | + config = WithProxy( |
| 87 | + proxy_url="socks5://proxy.example.com:1080", |
| 88 | + auth=("username", "password") # Optional |
| 89 | + ) |
| 90 | + transport = WebsocketTransport(upgrader, config=config) |
| 91 | +
|
| 92 | +**Using Environment Variables:** |
| 93 | + |
| 94 | +.. code-block:: python |
| 95 | +
|
| 96 | + from libp2p.transport.websocket import WithProxyFromEnvironment |
| 97 | +
|
| 98 | + # Reads HTTP_PROXY or HTTPS_PROXY environment variables |
| 99 | + config = WithProxyFromEnvironment() |
| 100 | + transport = WebsocketTransport(upgrader, config=config) |
| 101 | +
|
| 102 | +**Environment Variable Format:** |
| 103 | + |
| 104 | +.. code-block:: bash |
| 105 | +
|
| 106 | + export HTTP_PROXY=socks5://proxy.example.com:1080 |
| 107 | + export HTTPS_PROXY=socks5://proxy.example.com:1080 |
| 108 | +
|
| 109 | +AutoTLS Configuration |
| 110 | +--------------------- |
| 111 | + |
| 112 | +AutoTLS automatically generates and manages TLS certificates for browser integration: |
| 113 | + |
| 114 | +.. code-block:: python |
| 115 | +
|
| 116 | + from libp2p.transport.websocket import WithAutoTLS, AutoTLSConfig |
| 117 | +
|
| 118 | + autotls_config = AutoTLSConfig( |
| 119 | + enabled=True, |
| 120 | + default_domain="localhost", |
| 121 | + cert_validity_days=365 |
| 122 | + ) |
| 123 | +
|
| 124 | + config = WithAutoTLS(autotls_config) |
| 125 | + transport = WebsocketTransport(upgrader, config=config) |
| 126 | +
|
| 127 | +**Browser Integration:** |
| 128 | + |
| 129 | +AutoTLS is particularly useful for browser-based applications where you need trusted certificates for WebSocket connections. |
| 130 | + |
| 131 | +Advanced TLS Configuration |
| 132 | +--------------------------- |
| 133 | + |
| 134 | +For production deployments with custom certificates: |
| 135 | + |
| 136 | +.. code-block:: python |
| 137 | +
|
| 138 | + from libp2p.transport.websocket import WithAdvancedTLS |
| 139 | + from libp2p.transport.websocket.tls_config import ( |
| 140 | + TLSConfig, |
| 141 | + CertificateValidationMode |
| 142 | + ) |
| 143 | +
|
| 144 | + tls_config = TLSConfig( |
| 145 | + cert_file="path/to/cert.pem", |
| 146 | + key_file="path/to/key.pem", |
| 147 | + ca_file="path/to/ca.pem", |
| 148 | + validation_mode=CertificateValidationMode.STRICT |
| 149 | + ) |
| 150 | +
|
| 151 | + config = WithAdvancedTLS(tls_config) |
| 152 | + transport = WebsocketTransport(upgrader, config=config) |
| 153 | +
|
| 154 | +Connection Management |
| 155 | +--------------------- |
| 156 | + |
| 157 | +Configure connection limits and timeouts: |
| 158 | + |
| 159 | +.. code-block:: python |
| 160 | +
|
| 161 | + from libp2p.transport.websocket import ( |
| 162 | + WithMaxConnections, |
| 163 | + WithHandshakeTimeout |
| 164 | + ) |
| 165 | +
|
| 166 | + config = WebsocketConfig( |
| 167 | + max_connections=1000, |
| 168 | + handshake_timeout=15.0, |
| 169 | + max_buffered_amount=4 * 1024 * 1024, # 4MB |
| 170 | + max_message_size=32 * 1024 * 1024 # 32MB |
| 171 | + ) |
| 172 | +
|
| 173 | + transport = WebsocketTransport(upgrader, config=config) |
| 174 | +
|
| 175 | +**Monitoring Connection Statistics:** |
| 176 | + |
| 177 | +.. code-block:: python |
| 178 | +
|
| 179 | + # Get transport statistics |
| 180 | + stats = transport.get_stats() |
| 181 | + print(f"Total connections: {stats['total_connections']}") |
| 182 | + print(f"Current connections: {stats['current_connections']}") |
| 183 | + print(f"Failed connections: {stats['failed_connections']}") |
| 184 | +
|
| 185 | + # Get connection details |
| 186 | + connections = await transport.get_connections() |
| 187 | + for conn_id, conn in connections.items(): |
| 188 | + conn_stats = conn.get_stats() |
| 189 | + print(f"Connection {conn_id}: {conn_stats}") |
| 190 | +
|
| 191 | +Complete Example |
| 192 | +---------------- |
| 193 | + |
| 194 | +A complete example demonstrating WebSocket transport with all features: |
| 195 | + |
| 196 | +.. literalinclude:: ../examples/websocket/websocket_demo.py |
| 197 | + :language: python |
| 198 | + :linenos: |
| 199 | + |
| 200 | +Multiaddr Formats |
| 201 | +----------------- |
| 202 | + |
| 203 | +WebSocket transport supports various multiaddr formats: |
| 204 | + |
| 205 | +- **WS (Insecure):** ``/ip4/127.0.0.1/tcp/8080/ws`` |
| 206 | +- **WSS (Secure):** ``/ip4/127.0.0.1/tcp/8080/wss`` |
| 207 | +- **TLS/WS Format:** ``/ip4/127.0.0.1/tcp/8080/tls/ws`` |
| 208 | +- **IPv6:** ``/ip6/::1/tcp/8080/ws`` |
| 209 | +- **DNS:** ``/dns/example.com/tcp/443/wss`` |
| 210 | + |
| 211 | +Production Considerations |
| 212 | +-------------------------- |
| 213 | + |
| 214 | +1. **Security:** |
| 215 | + - Always use WSS in production |
| 216 | + - Use proper CA-signed certificates |
| 217 | + - Enable certificate validation |
| 218 | + - AutoTLS is for development/testing only |
| 219 | + |
| 220 | +2. **Performance:** |
| 221 | + - Configure appropriate connection limits |
| 222 | + - Set reasonable timeouts |
| 223 | + - Monitor connection statistics |
| 224 | + - Use connection pooling for high-traffic scenarios |
| 225 | + |
| 226 | +3. **Error Handling:** |
| 227 | + - Handle ``OpenConnectionError`` for connection failures |
| 228 | + - Implement retry logic for transient failures |
| 229 | + - Monitor failed connection counts |
| 230 | + |
| 231 | +4. **Proxy Configuration:** |
| 232 | + - Use environment variables for flexible deployment |
| 233 | + - Test proxy connectivity before production deployment |
| 234 | + - Monitor proxy connection statistics |
| 235 | + |
| 236 | +See Also |
| 237 | +-------- |
| 238 | + |
| 239 | +- :doc:`libp2p.transport.websocket` - Full API documentation |
| 240 | +- :doc:`examples.echo` - Basic echo protocol example |
| 241 | +- :doc:`examples.echo_quic` - QUIC transport example for comparison |
0 commit comments