Netty-based HTTP server implementation for the Play Framework providing high-performance, asynchronous HTTP processing with WebSocket support.
—
Core server instantiation, configuration, and lifecycle management capabilities for the Play Netty Server.
Create NettyServer instances from Play applications or route definitions.
/**
* Create a Netty server from a Play Application
* @param application The Play application to serve
* @param config Server configuration (defaults to ServerConfig())
* @return Started NettyServer instance
*/
def fromApplication(
application: Application,
config: ServerConfig = ServerConfig()
): NettyServer
/**
* Create a Netty server from route definitions
* @param config Server configuration
* @param routes Partial function mapping requests to handlers
* @return Started NettyServer instance
*/
def fromRouter(config: ServerConfig = ServerConfig())(
routes: PartialFunction[RequestHeader, Handler]
): NettyServerUsage Examples:
import play.api.Application
import play.core.server.{NettyServer, ServerConfig}
import play.api.mvc._
import play.api.routing.Router
// Create server from existing Play application
val app: Application = // ... your Play application
val server = NettyServer.fromApplication(app, ServerConfig(port = Some(9000)))
// Create server with custom routes
val server = NettyServer.fromRouter(ServerConfig(port = Some(8080))) {
case GET -> Root / "api" / "status" =>
Action { Ok("Server is running") }
case POST -> Root / "api" / "data" =>
Action(parse.json) { request =>
Ok(s"Received: ${request.body}")
}
}
// Server starts automatically when created
println(s"Server running on port ${server.httpPort.getOrElse("unknown")}")Main server implementation providing lifecycle management and connection information.
/**
* Netty-based HTTP server implementation
* @param config Server configuration including ports and SSL settings
* @param applicationProvider Provides access to the Play application
* @param stopHook Callback executed when server stops
*/
class NettyServer(
config: ServerConfig,
applicationProvider: ApplicationProvider,
stopHook: () => Future[Unit]
) extends Server {
/** Current server mode (Dev, Test, Prod) */
def mode: Mode
/** Stop the server and release all resources */
def stop(): Unit
/** HTTP port if configured */
def httpPort: Option[Int]
/** HTTPS port if configured */
def httpsPort: Option[Int]
/** Primary server address */
def mainAddress: InetSocketAddress
}Usage Examples:
import play.core.server.{NettyServer, ServerConfig}
import play.api.Mode
// Check server configuration
println(s"Server mode: ${server.mode}")
println(s"HTTP port: ${server.httpPort}")
println(s"HTTPS port: ${server.httpsPort}")
println(s"Main address: ${server.mainAddress}")
// Graceful shutdown
server.stop()Configure server ports, SSL settings, and other network parameters.
/**
* Server configuration for NettyServer
* @param port Optional HTTP port (None disables HTTP)
* @param sslPort Optional HTTPS port (None disables HTTPS)
* @param address Bind address (default: "0.0.0.0")
* @param mode Server mode (Dev/Test/Prod)
* @param configuration Play configuration for additional settings
*/
case class ServerConfig(
port: Option[Int] = None,
sslPort: Option[Int] = None,
address: String = "0.0.0.0",
mode: Mode = Mode.Prod,
configuration: Configuration = Configuration.empty
)Usage Examples:
import play.core.server.ServerConfig
import play.api.{Mode, Configuration}
// Basic HTTP-only server
val httpConfig = ServerConfig(port = Some(8080))
// HTTPS-only server
val httpsConfig = ServerConfig(sslPort = Some(8443))
// Both HTTP and HTTPS
val dualConfig = ServerConfig(
port = Some(8080),
sslPort = Some(8443),
address = "127.0.0.1"
)
// Development mode with custom configuration
val devConfig = ServerConfig(
port = Some(9000),
mode = Mode.Dev,
configuration = Configuration(
"play.server.netty.maxInitialLineLength" -> 8192,
"play.server.netty.maxHeaderSize" -> 8192
)
)Provides lazy access to Play applications with error handling.
/**
* Provides access to Play applications
*/
trait ApplicationProvider {
/** Get the application or error */
def get: Either[Throwable, Application]
/** Get current application if available */
def current: Option[Application]
}
/**
* Create application provider from instance
*/
object ApplicationProvider {
def apply(app: Application): ApplicationProvider
}Usage Examples:
import play.core.ApplicationProvider
// Create provider from application
val provider = ApplicationProvider(myApplication)
// Check application availability
provider.get match {
case Right(app) => println(s"Application available: ${app.configuration}")
case Left(error) => println(s"Application error: ${error.getMessage}")
}
// Get current application
provider.current.foreach { app =>
println(s"Current app mode: ${app.mode}")
}Configure Netty-specific server behavior through Play configuration.
// Available configuration keys under play.server.netty:
val nettyConfigKeys = Map(
"maxInitialLineLength" -> "Maximum URL length (default: 4096)",
"maxHeaderSize" -> "Maximum HTTP header size (default: 8192)",
"maxChunkSize" -> "Maximum chunk buffer size (default: 8192)",
"log.wire" -> "Enable wire-level logging (default: false)",
"option.child.keepAlive" -> "TCP keep-alive for connections",
"option.child.tcpNoDelay" -> "TCP no-delay flag",
"option.backlog" -> "TCP connection backlog size"
)Usage Examples:
// application.conf
play.server.netty {
maxInitialLineLength = 8192
maxHeaderSize = 16384
maxChunkSize = 16384
log.wire = true
option {
child.keepAlive = true
child.tcpNoDelay = true
backlog = 200
}
}
// Programmatic configuration
val config = Configuration(
"play.server.netty.maxInitialLineLength" -> 8192,
"play.server.netty.maxHeaderSize" -> 16384,
"play.server.netty.option.child.keepAlive" -> true
)
val serverConfig = ServerConfig(
port = Some(8080),
configuration = config
)Install with Tessl CLI
npx tessl i tessl/maven-com-typesafe-play--play-netty-server-2-10