The zio-tcp library implements for both client and server:
- non-blocking NIO2 backed TCP streams for pure ZStream implementations.
- Standard message exchange patterns
Supporting functionality:
- Host name resolution
- Peer-to-peer clustering library
zio-nio has the explicit goal of providing acccess to Java NIO functionality via a ZIO API.
ZIO has an implementation for a TCP server, that is currently incomplete and has life cycle management that is not suitable for a long lived server instance. There is no client implementation.
Echo server
import io.github.searler.zio_tcp.TCP
import zio._
object ServerExample extends App {
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
val program = for {
server <- TCP
.fromSocketServer(8888) // standard server listening on 8888 and wildcard bound
.mapMParUnordered(4)( //Execute up to 4 requests concurrently
TCP.handlerServer( // standard MEP
_ => // ignore the identity of the connecting client
Predef.identity)) // echo the request
.runDrain
} yield ()
program.exitCode
}
}
Client that makes a fixed request
import io.github.searler.zio_tcp.TCP
import zio._
object ClientExample extends App {
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] = {
val program = for {
conn <- TCP.fromSocketClient(8888, "localhost"). // connect to server
retry(Schedule.forever) // retry connection until successful
receive <- TCP.requestChunk( // Send Chunk[Byte] and receive a Chunk[Byte]
Chunk.fromArray("Request".getBytes()) // create request Chunk
)(conn) // request via connection
_ <- console.putStrLn(new String(receive.toArray))
} yield ()
program.exitCode
}
}
See the test cases for further examples
still to be implemented
- TLS
This repository captures functionality from ZIO pull requests that are not (yet) accepted:
The code was identical, modulo module name so there is no naming conflict. The implementation has since been improved and additional functionality has been provided.