UDP
The UDP connector provides Akka Stream flows that allow for sending and receiving UDP datagrams.
[+] Show project infoProject Info: Alpakka UDP | |
---|---|
Artifact | com.lightbend.akka
akka-stream-alpakka-udp
9.0.1
|
JDK versions | Eclipse Temurin JDK 11 Eclipse Temurin JDK 17 |
Scala versions | 2.13.12 |
JPMS module name | akka.stream.alpakka.udp |
License | |
Readiness level |
Since 0.20, 2018-07-04
|
Home page | https://doc.akka.io/libraries/alpakka/current |
API documentation | |
Forums | |
Release notes | GitHub releases |
Issues | Github issues |
Sources | https://github.com/akka/alpakka |
Artifacts
The Akka dependencies are available from Akka’s library repository. To access them there, you need to configure the URL for this repository.
Additionally, add the dependencies as below.
- sbt
val AkkaVersion = "2.10.0" libraryDependencies ++= Seq( "com.lightbend.akka" %% "akka-stream-alpakka-udp" % "9.0.1", "com.typesafe.akka" %% "akka-stream" % AkkaVersion )
- Maven
- Gradle
The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.
- Direct dependencies
Organization Artifact Version com.typesafe.akka akka-stream_2.13 2.10.0 org.scala-lang scala-library 2.13.12 - Dependency tree
Sending
Datagrams can be sent to remote destinations by using a Udp.sendFlow
or Udp.sendSink
which can be found in the Udp
factory object.
- Scala
-
source
val destination = new InetSocketAddress("my.server", 27015) val messagesToSend = 100 Source(1 to messagesToSend) .map(i => ByteString(s"Message $i")) .map(Datagram(_, destination)) .runWith(Udp.sendSink())
- Java
Receiving
First create an address which will be used to bind and listen for incoming datagrams.
A Flow created from Udp.bindFlow
will bind to the given address. All datagrams coming from the network to the bound address will be sent downstream. Datagrams received from the upstream will be sent to their corresponding destination addresses.
The flow materializes to the Future[InetSocketAddress]
which will eventually hold the address the flow was finally bound to.
- Scala
-
source
val bindFlow: Flow[Datagram, Datagram, Future[InetSocketAddress]] = Udp.bindFlow(bindToLocal)
- Java
Running the example code
The code in this guide is part of runnable tests of this project. You are welcome to browse the code, edit and run it in sbt.