diff --git a/websockets/socket.io/README.md b/websockets/socket.io/README.md new file mode 100644 index 0000000..ef0be5e --- /dev/null +++ b/websockets/socket.io/README.md @@ -0,0 +1,13 @@ +# **KrakenD Example with Socket.IO** + +This repository provides an example of how to integrate KrakenD with Socket.IO. +Please see the documentation here: https://www.krakend.io/docs/enterprise/websockets/#integrating-krakend-with-socketio + +> [Socket.IO](https://socket.io/) is a popular library to use bidirectional communication. Although Socket.IO name might sound as a WebSockets implementation, the reality is that Socket.IO operates on a **custom protocol** layered over WebSockets that is incompatible with plain WebSockets clients using the [WebSockets API](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications) (the one native in the JS standard lib). To connect to a Sockets.IO server you cannot use a WebSockets client, you must use a Sockets.IO client. + +> KrakenD uses a pure WebSocket Protocol (RFC-6455) to connect to servers, but the Socket.IO protocol requires specific signaling to establish and maintain connections. By default, it attempts to **use the same endpoint for both HTTP and WebSocket communication**, and the connection details passed on a query string (e.g., `?EIO=4&transport=websocket`). This design can cause confusion when integrating with KrakenD, which manages HTTP and WebSocket traffic separately. Make sure to use `websockets` only when passing through KrakenD. + +> Socket.IO also requires **dedicated connections for each client**. This approach is **incompatible with KrakenD's multiplexing system**, which optimizes resource usage by sharing WebSocket connections among multiple clients, so you are limited to use **direct WebSockets only**. Needles to say that handling individual client connections, leads to a much higher resource consumption. + +> Integrating KrakenD with Socket.IO can open up powerful real-time communication features, but it comes with trade-offs. The need for dedicated per-client connections, the additional dependency footprint, and challenges in maintaining asynchronous logic and multi-threaded execution must be considered before committing to this setup. + diff --git a/websockets/socket.io/index.html b/websockets/socket.io/index.html new file mode 100644 index 0000000..c8bf0af --- /dev/null +++ b/websockets/socket.io/index.html @@ -0,0 +1,59 @@ + + + + + Socket.IO chat + + + + +
+ +
+ + + + diff --git a/websockets/socket.io/krakend.json b/websockets/socket.io/krakend.json new file mode 100644 index 0000000..e862d9b --- /dev/null +++ b/websockets/socket.io/krakend.json @@ -0,0 +1,48 @@ +{ + "version": 3, + "port": 8080, + "endpoints": [{ + "endpoint": "/", + "output_encoding": "no-op", + "backend": [{ + "url_pattern": "/", + "host": ["localhost:3000"], + "encoding": "no-op" + }] + }, { + "endpoint": "/socket.io/socket.io.js", + "output_encoding": "no-op", + "backend": [{ + "url_pattern": "/socket.io/socket.io.js", + "host": ["localhost:3000"], + "encoding": "no-op" + }] + }, { + "endpoint": "/socket.io/", + "input_query_strings": ["*"], + "input_headers": ["*"], + "backend": [{ + "url_pattern": "/socket.io/?EIO=4&transport=websocket", + "disable_host_sanitize": true, + "host": [ + "ws://localhost:3000" + ] + }], + "extra_config": { + "websocket": { + "read_buffer_size": 4096, + "write_buffer_size": 4096, + "message_buffer_size": 4096, + "max_message_size": 3200000, + "write_wait": "10s", + "pong_wait": "60s", + "ping_period": "54s", + "max_retries": 0, + "backoff_strategy": "exponential", + "enable_direct_communication": true, + "disable_otel_metrics": true + } + } + + }] +} \ No newline at end of file