Integrating Play Framework 2.6 with gRPC and Netty - scala

At the time I write this post, Play Framework is at v2.6.0-M4. The v2.5 version of the framework had difficulties to work with gRPC because of Netty conflicts (see this stackoverflow answer).
I'm starting to look into gRPC and protobufs. Already ported a project from Play Framework 2.5 > 2.6.0-M4 in anticipation of the actual release. Currently I have some questions regarding integration of gRPC. I'm wondering how to let the gRPC server work along nicely with Play Framework. I know that v2.6 switched to Akka HTTP server instead of Netty, while I use the grpc-netty dependency in sbt so maybe I'll have to switch the project to Netty again (here's how).
For testing purposes, I created a quick and dirty GrpcServer.scala class that starts up a thread with the GrpcServer listening. I managed to add ScalaPB with gRPC and generate/compile my protobufs. It works perfectly to communicate with a small testing NodeJS app but I have to startup this server app independently from the main project:
private def start(): Unit = {
server = ServerBuilder.forPort(GrpcServer.port).addService(GreeterGrpc.bindService(new GreeterImpl, executionContext)).build.start
GrpcServer.logger.info("Server started, listening on " + GrpcServer.port)
sys.addShutdownHook {
System.err.println("*** shutting down gRPC server")
self.stop()
System.err.println("*** server shut down")
}
}
Possible solutions to integrate gRPC in Play Framework
Now for the real integration in Play Framework v2.6, I'm looking for suggestions. Here's some things I can do:
Create a module and start the gRPC server when Play Framework starts up, like described in this stackoverflow answer. This would mean that we run the gRPC server on a different port next to the existing server (Akka HTTP server from Play Framework 2.6)
Create a Scala Command and make it long-running. So we always make sure we start a command that runs the gRPC server when we start our application on a server.
Switch from Akka HTTP to Netty in Play Framework v2.6 and tightly integrate gRPC with the existing Netty server so it hooks into the existing Netty server instead of creating a server on our own. I would like this solution but not sure how to deal with it. It certainly would avoid having two separate http stacks running.
Any tips/ideas for a clean integration are helpful as there's not much information about Play Framework and gRPC available, except that there were issues in the previous 2.5 version...

Related

How to send HTTP 2.0 request in play framework(scala)

Backend running under play framework(v. 2.6.5 scala) must communicate with Apple Push Notificaton Service (APNs) . APNs requires using HTTP/2, and so i tried to find any way to implement such communication, but to my surprise i didn`t find any http scala clients, supporting http v 2.0.
Is there any way to realize such communication without going out of the framework?
Thanks!
Play Framework with a version prior to 2.6 does not support HTTP 2.0, but, Play Framework 2.6 is based on Akka-Http and has experimental support for HTTP 2.0. This feature is labeled "experimental" because the API may change in the future, and it has not been thoroughly tested in the wild.
To add support for HTTP/2, add the PlayAkkaHttp2Support plugin. You can do this in an enablePlugins:
lazy val root = (project in file("."))
.enablePlugins(PlayScala, PlayAkkaHttp2Support)
If you consider alternatives take a look at:
Jetty has a capability to use HTTP 2.0
Netty also supports HTTP 2.0
Check this list of known implementations of HTTP 2.0.
In Akka HTTP, which underlies Play! Framework, HTTP/2 support is experimental on the server side and not yet available on the client side, as far as I can tell. Work is in progress and is tracked on Github. Unless you're inclined to write HTTP/2 client support yourself and optionally donate it to the project (which would probably be a very satisfying experience), going outside of the framework is probably going to be a necessity for now, I'm afraid.
If you're in a position to use Java 9 (hey, it's been gold for two days!), you might consider its HTTP/2 client.
Vert.x is originally a Java framework inspired by NodeJS, but in its current incarnation it includes an HTTP/2 client with a Scala API: http://vertx.io/docs/vertx-web-client/scala/ - I've never used it in production so YMMV...
Edit: you might also consider sttp with the OkHttp backend, which supports HTTP/2.

Upgrade to webSocket message in play web sockets using scala

I am new to play framework and implementing web sockets using iteratees in Scala but when I run in browser it prints upgrade to Web sockets required with no compiler error. I have attached my snippet here. Please help. Am using Ubuntu platform.

Is akka-http fully compatible with spray-routing dsl?

Is akka-http fully compatible with spray-routing DSL? (my service is fully implemented in spray-routing trying to understand how seamless is migration (hopefully just dependency changing)
Is it production ready?
Can it run on tomcat like spray has Servlet30ConnectorServlet
Is there an example of how to run akka-http on tomcat container with similar Servlet30ConnectorServlet
No see https://www.linkedin.com/grp/post/746917-5967985951106945028
Not of this date yet the only package exist is akka-http-experimental but it appears it should be there soon.
No
Only by using tomcat as a simple start/stop for the akka-http service

Can I use socket.io with Play Framework 2.0 Web Sockets?

There's a sample application that comes with Play Framework 2.0. The application uses Play's WebSocket implementation on the server side. I am wondering can I use socket.io on the client side to connect with Play's WebSocket implementation on the server side?
In theory, yes, you can make Socket.io connect to your websocket server wia the corresponding ws url.
In practice, I tried and I had some issues, so it might require you to adapt Socket.io for it to work properly. Granted I tried on 2.0.1 or similar, so it may be working currently with no issues.

Best approach to integrate netty with openshift

In fact, I'm trying to see which would be the best approach to achieve play framework native support on openshift.
Play has it's own http server developed with netty. Right now you can deploy a play application to openshift, but you have to deploy it as a war, in which case play uses Servlet Container wrapper.
Being able to deploy it as a netty application would allow us to use some advanced features, like asynchronuos request.
Openshift uses jboss, so this question would also involve which would be the recommended approach to deploy a netty application on a jboss server, using netty instead of the servlet container provided by jboss.
Here is request for providing play framework native support on openshift There's more info there, and if you like it you can also add your vote ;-)
Start with creating 'raw-0.1' application.
SSH into the server and
cd $OPENSHIFT_DATA_DIR
download and install play into a directory here. $OPENSHIFT_DATA_DIR is supposed to survive redeploys of your application.
Now you can disconnect from SSH.
Clone the application repository. In the repository, there is a file .openshift/actions_hooks/start. It's task is to start the application using a framework of your choice. The file will need to contain at least (from what I know about Play)
cd $OPENSHIFT_REPO_DIR
$OPENSHIFT_DATA_DIR/play-directroy/play run --http.port=$OPENSHIFT_INTERNAL_PORT --some-other-parameters
Important
You have to bind to $OPENSHIFT_INTERNAL_IP:$OPENSHIFT_INTERNAL_PORT. Trying to bind to different interface is not allowed, also most of the ports are blocked.
To create some sort of template, save the installation steps into .openshift/action_hooks/build file. Check if play is installed, if it is do nothing, if it's not, execute the installation process.