Unix Domain Socket

From Wikipedia, A Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system. Unix Domain Sockets leverage files and so operating system level access control can be utilized. This is a security advantage over using TCP/UDP where IPC is required without a more complex Transport Layer Security (TLS). Performance also favors Unix Domain Sockets over TCP/UDP given that the Operating System’s network stack is bypassed.

This connector provides an implementation of a Unix Domain Socket with interfaces modelled on the conventional Tcp Akka Streams class. The connector uses JNI and so there are no native dependencies.

The binding and connecting APIs are extremely similar to the Tcp Akka Streams class. UnixDomainSocket is generally substitutable for Tcp except that the SocketAddress is different (Unix Domain Sockets requires a java.io.File as opposed to a host and port). Please read the following for details:

Note that Unix Domain Sockets, as the name implies, do not apply to Windows.

Project Info: Alpakka Unix Domain Socket
Artifact
com.lightbend.akka
akka-stream-alpakka-unix-domain-socket
1.0-M2
JDK versions
OpenJDK 8
Scala versions2.12.7, 2.11.12
JPMS module nameakka.stream.alpakka.unixdomainsocket
License
Readiness level
Community-driven
Since 0.16, 2018-01-11
Home pagehttps://doc.akka.io/docs/alpakka/current/
API documentation
Forums
Release notesIn the documentation
IssuesGithub issues
Sourceshttps://github.com/akka/alpakka

Artifacts

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-unix-domain-socket" % "1.0-M2"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-unix-domain-socket_2.12</artifactId>
  <version>1.0-M2</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-unix-domain-socket_2.12', version: '1.0-M2'
}

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

Direct dependencies
OrganizationArtifactVersionLicense
com.github.jnrjnr-unixsocket0.19The Apache Software License, Version 2.0
com.typesafe.akkaakka-stream_2.122.5.19Apache License, Version 2.0
org.scala-langscala-library2.12.7BSD 3-Clause
Dependency tree
com.github.jnr    jnr-unixsocket    0.19    The Apache Software License, Version 2.0
    com.github.jnr    jnr-constants    0.9.9    The Apache Software License, Version 2.0
    com.github.jnr    jnr-enxio    0.17    The Apache Software License, Version 2.0
        com.github.jnr    jnr-constants    0.9.9    The Apache Software License, Version 2.0
        com.github.jnr    jnr-ffi    2.1.4    The Apache Software License, Version 2.0
            com.github.jnr    jffi    1.2.15    The Apache Software License, Version 2.0
            com.github.jnr    jnr-x86asm    1.0.2    MIT License
            org.ow2.asm    asm-analysis    5.0.3    BSD
                org.ow2.asm    asm-tree    5.0.3    BSD
                    org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm-commons    5.0.3    BSD
                org.ow2.asm    asm-tree    5.0.3    BSD
                    org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm-tree    5.0.3    BSD
                org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm-util    5.0.3    BSD
                org.ow2.asm    asm-tree    5.0.3    BSD
                    org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm    5.0.3    BSD
    com.github.jnr    jnr-ffi    2.1.4    The Apache Software License, Version 2.0
        com.github.jnr    jffi    1.2.15    The Apache Software License, Version 2.0
        com.github.jnr    jnr-x86asm    1.0.2    MIT License
        org.ow2.asm    asm-analysis    5.0.3    BSD
            org.ow2.asm    asm-tree    5.0.3    BSD
                org.ow2.asm    asm    5.0.3    BSD
        org.ow2.asm    asm-commons    5.0.3    BSD
            org.ow2.asm    asm-tree    5.0.3    BSD
                org.ow2.asm    asm    5.0.3    BSD
        org.ow2.asm    asm-tree    5.0.3    BSD
            org.ow2.asm    asm    5.0.3    BSD
        org.ow2.asm    asm-util    5.0.3    BSD
            org.ow2.asm    asm-tree    5.0.3    BSD
                org.ow2.asm    asm    5.0.3    BSD
        org.ow2.asm    asm    5.0.3    BSD
    com.github.jnr    jnr-posix    3.0.45    Eclipse Public License - v 1.0
        com.github.jnr    jnr-constants    0.9.9    The Apache Software License, Version 2.0
        com.github.jnr    jnr-ffi    2.1.4    The Apache Software License, Version 2.0
            com.github.jnr    jffi    1.2.15    The Apache Software License, Version 2.0
            com.github.jnr    jnr-x86asm    1.0.2    MIT License
            org.ow2.asm    asm-analysis    5.0.3    BSD
                org.ow2.asm    asm-tree    5.0.3    BSD
                    org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm-commons    5.0.3    BSD
                org.ow2.asm    asm-tree    5.0.3    BSD
                    org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm-tree    5.0.3    BSD
                org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm-util    5.0.3    BSD
                org.ow2.asm    asm-tree    5.0.3    BSD
                    org.ow2.asm    asm    5.0.3    BSD
            org.ow2.asm    asm    5.0.3    BSD
com.typesafe.akka    akka-stream_2.12    2.5.19    Apache License, Version 2.0
    com.typesafe.akka    akka-actor_2.12    2.5.19    Apache License, Version 2.0
        com.typesafe    config    1.3.3    Apache License, Version 2.0
        org.scala-lang.modules    scala-java8-compat_2.12    0.8.0    BSD 3-clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    com.typesafe.akka    akka-protobuf_2.12    2.5.19    Apache License, Version 2.0
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    com.typesafe    ssl-config-core_2.12    0.3.6    Apache-2.0
        com.typesafe    config    1.3.3    Apache License, Version 2.0
        org.scala-lang.modules    scala-parser-combinators_2.12    1.1.1    BSD 3-clause
            org.scala-lang    scala-library    2.12.7    BSD 3-Clause
        org.scala-lang    scala-library    2.12.7    BSD 3-Clause
    org.reactivestreams    reactive-streams    1.0.2    CC0
    org.scala-lang    scala-library    2.12.7    BSD 3-Clause
org.scala-lang    scala-library    2.12.7    BSD 3-Clause

Binding to a file

Scala
val file: java.io.File = // ...
val binding: Future[UnixDomainSocket.ServerBinding] =
  UnixDomainSocket().bindAndHandle(Flow.fromFunction(identity), file)
Java
java.io.File file = // ...
final Source<IncomingConnection, CompletionStage<ServerBinding>> connections =
    UnixDomainSocket.get(system).bind(file);

Connecting to a file

Scala
binding.flatMap { connection =>
  val sendBytes = ByteString("Hello")
  val result: Future[ByteString] =
    Source
      .single(sendBytes)
      .via(UnixDomainSocket().outgoingConnection(file))
      .runWith(Sink.head)
}
Java
connections.runForeach(
    connection -> {
      System.out.println("New connection from: " + connection.remoteAddress());

      final Flow<ByteString, ByteString, NotUsed> echo =
          Flow.of(ByteString.class)
              .via(
                  Framing.delimiter(
                      ByteString.fromString("\n"), 256, FramingTruncation.DISALLOW))
              .map(ByteString::utf8String)
              .map(s -> s + "!!!\n")
              .map(ByteString::fromString);

      connection.handleWith(echo, materializer);
    },
    materializer);
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.