{"id":19506822,"url":"https://github.com/calvinlfer/akka-http-streaming-response-examples","last_synced_at":"2025-04-26T02:32:54.215Z","repository":{"id":68736706,"uuid":"68766159","full_name":"calvinlfer/akka-http-streaming-response-examples","owner":"calvinlfer","description":"A list of examples that involve streaming with Akka Streams and used together with Akka HTTP ","archived":false,"fork":false,"pushed_at":"2017-10-25T23:14:24.000Z","size":42,"stargazers_count":73,"open_issues_count":2,"forks_count":20,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-21T12:41:21.281Z","etag":null,"topics":["actors","akka-http","akka-streams","chat-room","streaming-json","streaming-response","websocket-connection"],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/calvinlfer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-21T01:05:28.000Z","updated_at":"2024-07-20T23:31:09.000Z","dependencies_parsed_at":"2023-05-30T06:00:27.253Z","dependency_job_id":null,"html_url":"https://github.com/calvinlfer/akka-http-streaming-response-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calvinlfer%2Fakka-http-streaming-response-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calvinlfer%2Fakka-http-streaming-response-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calvinlfer%2Fakka-http-streaming-response-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/calvinlfer%2Fakka-http-streaming-response-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/calvinlfer","download_url":"https://codeload.github.com/calvinlfer/akka-http-streaming-response-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250922131,"owners_count":21508280,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["actors","akka-http","akka-streams","chat-room","streaming-json","streaming-response","websocket-connection"],"created_at":"2024-11-10T22:38:37.429Z","updated_at":"2025-04-26T02:32:54.209Z","avatar_url":"https://github.com/calvinlfer.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Examples using Akka HTTP with Streaming\nA list of examples that involve streaming with Akka Streams and used together with Akka HTTP. \nThe Akka-HTTP specific details are isolated into the following traits: \n\n- [Chunked Streaming Routes](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala)\n- [WebSocket Routes](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/ws/WebSocketRoutes.scala)\n\nThe initialization logic to start the Akka HTTP server is kept in [ServerMain](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/ServerMain.scala).\n\nFocusing on HTTP Chunked Streaming Routes:\n\n- [`streaming-text`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L33) - This uses predefined Sources and throttling in order to give the user a chunked response in a controlled manner\n- [`actor-text`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L50) - This is more involved, we define an [Actor that respects Akka Streams backpressure](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/BackpressuredActor.scala) and then create a Source from this. \nThe Akka Scheduler constantly tells the Actor to emit messages into the Stream that the user sees. This logic is placed within the Actor. If you \nuse a web browser and stop the streaming response then you will cancel the stream and shut down the actor. Feel free to open this up on multiple\nbrowsers since we allocate an actor per request\n- [`alt-actor-text`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L60) - This is similar to `actor-text` except it uses `mapMaterializedValue` \nto access the materialized ActorRef and schedules messages to be sent constantly (in addition to the scheduler sending messages inside the Actor)\n- [`live-actor-text`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L74) - This is slightly different from the other `actor` endpoints. \nIt creates a live actor and places it into a Publisher and creates a Source from this. We publish messages in the same way as the previous examples\n- [`streaming-json`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L100) - This is an example of a JSON streaming route. We define a JSON Marshaller for a case class [here](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/DetailedMessage.scala) and we add a few imports for [streaming support](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L92) in order to have streaming JSON. You can customize the separators as well. \n- [`consuming-streaming-json`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L112) - This is an example of an endpoint that consumes a Streaming JSON request. This also relies on having JSON Streaming support.\n- [`streaming-sse-json`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/chunked/http/ChunkedStreamingRoutes.scala#L125) - This is an example of Streaming JSON using [Server Sent Events](http://www.html5rocks.com/en/tutorials/eventsource/basics/) with the help of Heiko Seeberger's [Akka-SSE](https://github.com/hseeberger/akka-sse) library. SSEs have better integration with modern day browsers.\n\nFocusing on the WebSocket Routes:\n\n- [`ws-simple`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/ws/WebSocketRoutes.scala#L23) - This can be accessed via `ws://localhost:9000/ws-simple`, it uses a stateless Akka Streams Flow in order to echo back the message. \nThe input to the Flow represents the WebSocket messages coming from the WebSocket Client and the output to the Flow represents the messages that are being sent to the WebSocket Client\n- [`ws-chat`](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/ws/WebSocketRoutes.scala#L87) - This is an implementation of an online chat-room, it shows how to integrate Actors with Streams in order to create it. All credits go to [Johan Andrén](https://markatta.com/codemonkey/blog/2016/04/18/chat-with-akka-http-websockets/) \nfor his excellent work and explanations on setting this up. This example involves creating a Flow from a Source and a Sink. I have provided my explanation and a [diagram](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/ws/WebSocketRoutes.scala#L76)\nto help you understand how this works. Essentially a Flow can be modelled as a Sink and a Source underneath the hood as illustrated by the diagram. If you choose to go \nabout thinking in this manner then you have full control over the Flow (as in what the Flow accepts and what the Flow emits). We use Actors underneath the hood to perform\nthe coordination between the accepting and emitting of Flow controlling messages to and from WebSocket clients. \n\n## Chat Room Flow construction Overview ##\nWebSocket Clients connect to the Chat-Room via `ws://localhost:9000/ws-chat` and a Flow is created. Let's take a look at the inner workings of this Flow:\n\n- First an intermediate Actor is created (per WebSocket Client connection) that is meant to act as the bridge between the WebSocket Actor (more on this below) and the Chat Room Actor\n- The Flow is composed from a Sink and a Source. Take a look at the [diagram](https://github.com/calvinlfer/akka-http-streaming-response-examples/blob/master/src/main/scala/com/experiments/calvin/ws/WebSocketRoutes.scala#L76) before coming back here. \n    - The Sink (inside the Flow) represents messages coming in from WebSocket clients, we use Sink.actorRef(intermediate Actor) so that the intermediate Actor will now receive messages whenever the WebSocket Source sends us messages\n    - The Source (inside the Flow) represents messages sent to the WebSocket clients, we use Source.actorRef along with `mapMaterializedValue` to get access to the materialized ActorRef that we use to send messages to, in order for messages to be sent to the WebSocket client\n- We create a Flow from the Sink and the Source which now represents each WebSocket connection between our server and the WebSocket clients\n\n**Note:** Websocket clients can be found here: [Online WebSocket Tester](https://www.websocket.org/echo.html), [Dark WebSocket Client](https://chrome.google.com/webstore/detail/dark-websocket-terminal/dmogdjmcpfaibncngoolgljgocdabhke), etc.\n\n## Contributions and PRs\n\nPlease feel free to send in PRs and contributions and we can review them together and see whether they are small and cohesive enough to fit into the project\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalvinlfer%2Fakka-http-streaming-response-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalvinlfer%2Fakka-http-streaming-response-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalvinlfer%2Fakka-http-streaming-response-examples/lists"}