Code Monkey home page Code Monkey logo

akka-http-akka-streams-akka-actors-integration's Introduction

Akka HTTP + Akka Streams + Akka Actors Integration Example

This application is able to respond with a Streaming JSON response of Chuck Norris jokes taken from here

Purpose

The purpose of this application was to help familiarize myself (and anyone interested) with how Akka Streams can integrate with Akka Actors and Akka HTTP. Also, I was bored and looking for something fun to do ๐Ÿ˜„.

Problem Statement

The Internet Chuck Norris Database returns a discrete JSON response for every HTTP request sent out. Whilst this is okay for most cases, it becomes a bit cumbersome if you want to look at Chuck Norris jokes as you have to keep hitting the endpoint yourself. The goal of this project is to turn those discretized JSON responses into a continuous JSON stream. This is done with the help of Akka HTTP, Akka Actors and Akka Streams.

Overview

The JokeFetcher Actor

The JokeFetcher Akka Actor is used to continuously poll the ICNDB endpoint behind the scenes and publish each discrete JSON response onto the Event Stream as a JokeEvent.

The JokePublisher Actor (the integration point with Akka Streams)

The JokePublisher Akka Actor is a point of integration between Akka Actors and Akka Streams. The JokePublisher is created whenever a request to the /streaming-jokes endpoint which in turn creates an Akka Stream to stream the response back to the requester. It lives for the duration of that Stream and gets terminated once the Stream completes (does not happen in this case because it is an infinite Stream) or when the Stream is cancelled (happens when the requester does not want anymore data). As you can see the JokePublisher is tied to the duration of each Stream. This means if 6 users come in and request data from /streaming-jokes then 6 actors of JokePublisher will be created, each of them will be responsible for publishing data into that specific user's streaming response. The JokePublisher on creation, will subscribe to the Akka EventStream and listen for JokeEvents and send them downstream to provide streaming responses. As you can see, publishing messages to the JokePublisher Actor will end up in the Akka Stream which is why this is a point of integration between Akka Actors and Akka Streams when it comes to publishing data into the Akka Stream. This actor respects backpressure. It will only send information as fast as the downstream consumer can consume. It will drop messages if they are coming in too quickly.

Endpoints

Users can hit the /streaming-jokes route in order to get back an SSE streaming JSON response of Chuck Norris jokes in JSON format.

Pre-requisites:

  • Scala 2.11.8
  • SBT

Instructions:

  • sbt run to start the application
  • Visit localhost:9000/streaming-jokes to see the Streaming response

Consumption of Streaming SSE JSON with a JavaScript client

Here is an example of how to consume the Stream using JavaScript

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Consuming Streaming JSON</title>
	</head>

	<body></body>
	<script>
	var body = document.body;
	var eventSource = new EventSource("http://localhost:9000/streaming-jokes");
	eventSource.addEventListener('jsonJoke', function(event) {
		var joke = JSON.parse(event.data);
		body.innerHTML += joke.message + "<br/>";
	});
	</script>
</html>

The above example will print out the SSE Events received from the server in the browser. You may need to use the CORS Chrome Addon

Preview:

streaming

Credits:

akka-http-akka-streams-akka-actors-integration's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.