I need to build REST API for my services and I am new to Scala. I have come to know of finatra and akka-http. After studying for a while, I am more inclined towards finatra as it offers java-style syntax and methodology for web services. However, akka has a very close knit relation to scala and learning akka-http at this point might help me in my scala journey.
Any help is appreciated.
They are on very different levels, akka-http is mainly a http server / client with very raw implementation and finatra is framework with modules, dependency injection, you get an administration view and page, flags and configuration. Finatra is the way to go if you like to have a more functional library then have a look at Finch https://finagle.github.io/finch/
Related
I recently started learning Akka-HTTP and struggling to get one answer.
We know that libraries like Jersey, RestEasy follows a common interface/APIs which is JAX-RS.
And Akka-HTTP is also a suite of libraries. So, does Akka-HTTP also follow JAX-RS?
If not, then how is Akka-HTTP different from JAX-RS?
Any answer to it will be highly appreciated. Thanks in advance.
JAX-RS is just a specification; Jersey, RestEasy etc. are just different implementations of this specification.
Akka-http does not implement JAX-RS; furthermore, equivalent mappings in JAX-RS and Akka-http would be written in completely different code styles:
API's written with JAX-RS are regular methods annotated with annotations
API's written with Akka-http are using router functions instead of annotations and communicate with actors (more about actor model here)
I have couple of questions related to building high-scalable REST-API is scala domain.
What is the recommended way of building high-scalable REST-API , spray.io or PLAY2-MVC
Since Spray.io is getting converted to Akka-http, we have double mind on using the spray-library
Is Play.MVC.Controllers actor based system, coz I don;t see actor reference in the class
trait Controller extends Results with BodyParsers with HttpProtocol with Status with HeaderNames with ContentTypes with RequestExtractors with Rendering
Thoughts please
Play is a full stack framework (similar to RoR, but without many of the weakness's that RoR has). Play (by far) has the best support for client side tooling (i.e. stuff like coffee/less/require.js integration)
On the other hand, if you are strictly doing an API, akka-http/spray may be a better choice.
Akka-http is easy to understand and is quite scalable , so I would recommend using Akka-http.
For the answer of your third ques: Yes Play is itself built on akka : How is Akka used in Play?
For more info you can see this reddit page where many have given it a try: https://www.reddit.com/r/scala/comments/3qyg2m/which_should_i_use_to_build_my_restful_api/
I am somewhat new to the domain of web development. I am investigating Play2 and am trying to understand, do you need some kind of JS frontend framework to go with Play2 and Scala?
I notice that Play2 has a template engine, but it seems that it generates the HTML on the server and sends it to the browser. Does this mean that the need for a JS frontend like Angular2 is made irrelevant? Or is there still a reason to use Angular2 in a Play2 application? What instances would it make sense and why?
I have some experience of using Play 2 with AngularJS (currently rewriting frontend to Angular2).
I use Play 2 (scala) only for RESTful JSON backend, which is just great for this purpose and I use Angular for single page application frontend. I find this combination brilliant.
My project is hosted on GitHub, you can check it out here
There are three directories in the root:
restful with all backend stuff, written in scala, using Slick for DB,
taking advantage of Play evolutions and all other stuff.
angularjs-client-deprecated with AngularJS code. I used angular-resource for making calls to backend.
client with Angular2 code. There is nothing there so far except for my experiments, however I've written a simple service for authentication with security token.
To summarise, I've been developing my project for almost 2 months so far, mainly to learn both Scala and AngularJS (now Angular2).
If you're planning to use JS framework for frontend, I would recommend you to use Play2 only for RESTFul Json backend. I don't see a reason to use Play2 html template engine in this case.
Play is more about Server Side templating, which isn't all that useful if you're using Angular. It can be done, but it comes with some overhead. I've worked with Scalatra in the past and it worked really well for me.
http://scalatra.org/
Other options include Spray, which is very similar from the outside, but uses Akka internally and uses non-blocking I/O, similar to Play!
http://spray.io/
With angular it is better to go with simple rest/http api like Akka-IO or spray for data and other stuff. Play will be less relevant with Angular.
As others have pointed out Play2 isn't really the best option for building responsive websites. I have a seed project which can help you get started with Akka Http and Angular 5...already configured to deploy to Heroku in a single web dyno.
https://github.com/jdschmitt/akka-angular-heroku
Check it out. I hope it helps anyone landing here looking for a way to manage back-end and front-end in a single repo for simple projects.
I have an API service implemented in a Play 2 App, Currently the api handles 5-7000 requests per second on 5 servers. I have benchmarked Spray.IO and Play2 recently and see almost 3x the performance on simple http requests.
I would prefer not to re-implement everything in Spray, if instead I can somehow embed the spray HTTP server in my Play app, than use that to server http requests.
Is this possible? and is there an example of this out there?
I can say that Spray actually is the fastest JVM-based toolkit for web-based development, you can check out the latest benchmarks on the official blog.
As for the question. If you want to write your own implementation for a little HTTP server then you should check spray-can http based api, spray-io is just a layer between Akka IO and Java NIO. I'm not very good at Play, but as a way i would sugest to create a multi-build sbt config or separate project with Spray http server and connect them through REST api.
Architecture would be pretty simple cause it's based on Akka actors in the simplest case would look like a bunch of cases in the receive method:
def receive = {
case HttpRequest(GET, Uri.Path("/ping"), _, _, _) =>
sender ! HttpResponse(entity = "PONG")
}
On the Play side you should use Akka or Play's routing file for describing routes.
Also check out Spray routing api. It's much easier to implement and to use and looks very similar to what Spray can give you with route file, but more flexible and readable.
Also Spray has many great very detailed examples on their Github repo: spray-can and spray-routing.
BTW Spray team also made available Play template engine for front-end and called it Twirl so porting your application to Spray won't take much time
We see that Play rates well for a full stack framework:
http://www.techempower.com/benchmarks/#section=data-r6&hw=i7&test=db&c=1&d=2&a=1
We see that there are trade-offs in terms of developer productivity i.e. the faster you want to go, the less productive your developers will typically be. You need to know whether Play will achieve the performance that your application demands.
Incidentally we have been focused and continue to focus on improving Play's performance.
I hope that this helps.
Actually, The Play framework will soon be using Spray I/O for it's http serverices.
So it looks like you'll be able to have the best of both worlds!
Let's say I have some service that talks over ZeroMQ sockets, and I want to provide access to that service to a single-page web application. I'd like the web app to talk to a service that provides a REST API (for control and queries) and WebSockets (for monitoring), and which does this by talking ZeroMQ to the first service. I'd like to write this in Scala.
What options are available to me for building that second service?
A very integrated solution would be to use Akka/Play2 for this.
Akka would be the core component talking to the ZeroMQ socktes via akkas ZeroMQ Module, which gives you a nice Scala-API and Akka/Actor integration. This Akka/Actor system can than be accessed via HTTP/WebSockets by using either play-mini or play2 which mainly differ in the style of defining HTTP endpoints.
For REST API I would recommend Spray - a nice library with a very concise and flexible DSL for defining web services. We've integrated Spray into our current project and are pretty happy with it. As for play-mini, AFAIK, it depends on the entire play2 project, so you'll end up with a lot of stuff you don't need.