0
# Play Netty Server
1
2
Play Netty Server is a high-performance, asynchronous HTTP server implementation built on Netty 3.x that serves as the default server backend for Play Framework applications. It provides comprehensive HTTP/HTTPS request processing, WebSocket support, SSL/TLS termination, and seamless integration with Play's reactive architecture.
3
4
## Package Information
5
6
- **Package Name**: play-netty-server_2.10
7
- **Package Type**: maven
8
- **Language**: Scala
9
- **Installation**: `libraryDependencies += "com.typesafe.play" %% "play-netty-server" % "2.4.11"`
10
11
## Core Imports
12
13
```scala
14
import play.core.server.NettyServer
15
import play.core.server.NettyServerProvider
16
import play.core.server.NettyServerComponents
17
```
18
19
## Basic Usage
20
21
```scala
22
import play.api.Application
23
import play.core.server.{NettyServer, ServerConfig}
24
import play.api.mvc.{RequestHeader, Handler}
25
import play.api.routing.Router
26
27
// Create server from Application
28
val server = NettyServer.fromApplication(application, ServerConfig())
29
30
// Create server from routes
31
val server = NettyServer.fromRouter() {
32
case request @ GET -> Root / "hello" =>
33
Action { Ok("Hello World") }
34
}
35
36
// Stop the server
37
server.stop()
38
```
39
40
## Architecture
41
42
Play Netty Server is built around several key components:
43
44
- **NettyServer**: Main server class handling HTTP/HTTPS connections and lifecycle management
45
- **Request Pipeline**: Multi-stage processing pipeline with handlers for HTTP parsing, WebSocket upgrades, and request routing
46
- **Response Streaming**: Efficient streaming of Play Results back to clients with support for chunked encoding and connection management
47
- **WebSocket Support**: Full WebSocket protocol implementation with frame handling and flow control
48
- **SSL/TLS Integration**: Built-in SSL/TLS support using Play's SSLEngine infrastructure
49
50
## Capabilities
51
52
### Server Creation and Management
53
54
Core server instantiation, configuration, and lifecycle management. Provides factory methods for creating servers from applications or route definitions.
55
56
```scala { .api }
57
object NettyServer {
58
def fromApplication(
59
application: Application,
60
config: ServerConfig = ServerConfig()
61
): NettyServer
62
63
def fromRouter(config: ServerConfig = ServerConfig())(
64
routes: PartialFunction[RequestHeader, Handler]
65
): NettyServer
66
67
implicit val provider: NettyServerProvider
68
}
69
70
class NettyServer(
71
config: ServerConfig,
72
applicationProvider: ApplicationProvider,
73
stopHook: () => Future[Unit]
74
) extends Server {
75
def mode: Mode.Mode
76
def stop(): Unit
77
def httpPort: Option[Int]
78
def httpsPort: Option[Int]
79
def mainAddress: java.net.InetSocketAddress
80
}
81
```
82
83
[Server Management](./server-management.md)
84
85
### Server Components
86
87
Cake pattern components for building and configuring Netty servers with dependency injection support.
88
89
```scala { .api }
90
trait NettyServerComponents {
91
lazy val serverConfig: ServerConfig
92
lazy val server: NettyServer
93
lazy val environment: Environment
94
lazy val configuration: Configuration
95
def application: Application
96
def serverStopHook: () => Future[Unit]
97
}
98
99
class NettyServerProvider extends ServerProvider {
100
def createServer(context: ServerProvider.Context): NettyServer
101
}
102
```
103
104
[Server Components](./server-components.md)
105
106
### WebSocket Support
107
108
Full WebSocket protocol implementation with handshake handling, frame processing, and flow control for real-time communication.
109
110
```scala { .api }
111
// Internal WebSocket handling (used by Play framework)
112
trait WebSocketHandler {
113
def websocketHandshake[A](
114
ctx: ChannelHandlerContext,
115
req: HttpRequest,
116
e: MessageEvent,
117
bufferLimit: Long
118
)(frameFormatter: FrameFormatter[A]): Enumerator[A]
119
120
def websocketable(req: HttpRequest): WebSocketable
121
}
122
```
123
124
[WebSocket Support](./websocket-support.md)
125
126
## Types
127
128
```scala { .api }
129
// Core server configuration
130
case class ServerConfig(
131
rootDir: java.io.File,
132
port: Option[Int],
133
sslPort: Option[Int],
134
address: String,
135
mode: Mode.Mode,
136
properties: java.util.Properties,
137
configuration: Configuration
138
)
139
140
object ServerConfig {
141
def apply(
142
classLoader: ClassLoader = ServerConfig.getClass.getClassLoader,
143
rootDir: java.io.File = new java.io.File("."),
144
port: Option[Int] = Some(9000),
145
sslPort: Option[Int] = None,
146
address: String = "0.0.0.0",
147
mode: Mode.Mode = Mode.Prod,
148
properties: java.util.Properties = System.getProperties
149
): ServerConfig
150
}
151
152
// Server provider context
153
case class ServerProvider.Context(
154
config: ServerConfig,
155
appProvider: ApplicationProvider,
156
actorSystem: akka.actor.ActorSystem,
157
stopHook: () => Future[Unit]
158
)
159
160
// Application provider for lazy loading
161
trait ApplicationProvider {
162
def get: scala.util.Try[Application]
163
def current: Option[Application]
164
def handleWebCommand(requestHeader: RequestHeader): Option[Result]
165
}
166
167
object ApplicationProvider {
168
def apply(application: Application): ApplicationProvider
169
}
170
171
// Base server trait
172
trait Server {
173
def mode: Mode.Mode
174
def applicationProvider: ApplicationProvider
175
def stop(): Unit
176
def httpPort: Option[Int]
177
def httpsPort: Option[Int]
178
def mainAddress: java.net.InetSocketAddress
179
def getHandlerFor(request: RequestHeader): Either[Future[Result], (RequestHeader, Handler, Application)]
180
}
181
182
// Play modes
183
object Mode {
184
sealed trait Mode
185
case object Dev extends Mode
186
case object Test extends Mode
187
case object Prod extends Mode
188
}
189
```