UDP

The UDP connector provides Akka Stream flows that allow for sending and receiving UDP datagrams.

Project Info: Alpakka UDP
Artifact
com.lightbend.akka
akka-stream-alpakka-udp
3.0.4+59-f1dbbcb3-SNAPSHOT
JDK versions
Adopt OpenJDK 8
Adopt OpenJDK 11
Scala versions2.13.8
JPMS module nameakka.stream.alpakka.udp
License
Readiness level
Since 0.20, 2018-07-04
Home pagehttps://doc.akka.io/docs/alpakka/current
API documentation
Forums
Release notesGitHub releases
IssuesGithub issues
Sourceshttps://github.com/akka/alpakka

Artifacts

sbt
val AkkaVersion = "2.6.19"
libraryDependencies ++= Seq(
  "com.lightbend.akka" %% "akka-stream-alpakka-udp" % "3.0.4+59-f1dbbcb3-SNAPSHOT",
  "com.typesafe.akka" %% "akka-stream" % AkkaVersion
)
Maven
<properties>
  <akka.version>2.6.19</akka.version>
  <scala.binary.version>2.13</scala.binary.version>
</properties>
<dependencies>
  <dependency>
    <groupId>com.lightbend.akka</groupId>
    <artifactId>akka-stream-alpakka-udp_${scala.binary.version}</artifactId>
    <version>3.0.4+59-f1dbbcb3-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>com.typesafe.akka</groupId>
    <artifactId>akka-stream_${scala.binary.version}</artifactId>
    <version>${akka.version}</version>
  </dependency>
</dependencies>
Gradle
def versions = [
  AkkaVersion: "2.6.19",
  ScalaBinary: "2.13"
]
dependencies {
  implementation "com.lightbend.akka:akka-stream-alpakka-udp_${versions.ScalaBinary}:3.0.4+59-f1dbbcb3-SNAPSHOT"
  implementation "com.typesafe.akka:akka-stream_${versions.ScalaBinary}:${versions.AkkaVersion}"
}

The table below shows direct dependencies of this module and the second tab shows all libraries it depends on transitively.

Direct dependencies
OrganizationArtifactVersion
com.typesafe.akkaakka-stream_2.132.6.19
org.scala-langscala-library2.13.8
Dependency tree
com.typesafe.akka    akka-stream_2.13    2.6.19    Apache-2.0
    com.typesafe.akka    akka-actor_2.13    2.6.19    Apache-2.0
        com.typesafe    config    1.4.2    Apache-2.0
        org.scala-lang.modules    scala-java8-compat_2.13    1.0.0    Apache-2.0
            org.scala-lang    scala-library    2.13.8    Apache-2.0
        org.scala-lang    scala-library    2.13.8    Apache-2.0
    com.typesafe.akka    akka-protobuf-v3_2.13    2.6.19    Apache-2.0
    com.typesafe    ssl-config-core_2.13    0.4.3    Apache-2.0
        com.typesafe    config    1.4.2    Apache-2.0
        org.scala-lang.modules    scala-parser-combinators_2.13    1.1.2    Apache-2.0
            org.scala-lang    scala-library    2.13.8    Apache-2.0
        org.scala-lang    scala-library    2.13.8    Apache-2.0
    org.reactivestreams    reactive-streams    1.0.3    CC0
    org.scala-lang    scala-library    2.13.8    Apache-2.0
org.scala-lang    scala-library    2.13.8    Apache-2.0

Sending

Datagrams can be sent to remote destinations by using a Udp.sendFlow or Udp.sendSink which can be found in the UdpUdp 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
source  final InetSocketAddress destination = new InetSocketAddress("my.server", 27015);
final Integer messagesToSend = 100;

Source.range(1, messagesToSend)
    .map(i -> ByteString.fromString("Message " + i))
    .map(bs -> Datagram.create(bs, destination))
    .runWith(Udp.sendSink(system), system);

Receiving

First create an address which will be used to bind and listen for incoming datagrams.

Scala
sourceval bindToLocal = new InetSocketAddress("localhost", 0)
Java
sourcefinal InetSocketAddress bindToLocal = new InetSocketAddress("localhost", 0);

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]CompletionStage<InetSocketAddress> which will eventually hold the address the flow was finally bound to.

Scala
sourceval bindFlow: Flow[Datagram, Datagram, Future[InetSocketAddress]] =
  Udp.bindFlow(bindToLocal)
Java
sourcefinal Flow<Datagram, Datagram, CompletionStage<InetSocketAddress>> bindFlow =
    Udp.bindFlow(bindToLocal, system);

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.

Scala
sbt
> udp/testOnly *.UdpSpec
Java
sbt
> udp/testOnly *.UdpTest
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.