Need help with swift-service-discovery?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

163 Stars 6 Forks Apache License 2.0 30 Commits 2 Opened issues


A service discovery API for Swift.

Services available


Need anything else?

Contributors list

Swift Service Discovery

A Service Discovery API for Swift.

Service discovery is how services locate one another within a distributed system. This API library is designed to establish a standard that can be implemented by various service discovery backends such as DNS-based, key-value store like Zookeeper, etc. In other words, this library defines the API only, similar to SwiftLog and SwiftMetrics; actual functionalities are provided by backend implementations.

This is the beginning of a community-driven open-source project actively seeking contributions, be it code, documentation, or ideas. Apart from contributing to SwiftServiceDiscovery itself, we need SwiftServiceDiscovery-compatible libraries which manage service registration and location information for querying. What SwiftServiceDiscovery provides today is covered in the API docs, but it will continue to evolve with community input.

Getting started

If you have a server-side Swift application and would like to locate other services within the same system for making HTTP requests or RPCs, then SwiftServiceDiscovery is the right library for the job. Below you will find all you need to know to get started.


  • Service Identity: Each service must have a unique identity.
    denotes the identity type used in a backend implementation.
  • Service Instance: A service may have zero or more instances, each of which has an associated location (typically host-port).
    denotes the service instance type used in a backend implementation.

Selecting a service discovery backend implementation (applications only)

Note: If you are building a library, you don't need to concern yourself with this section. It is the end users of your library (the applications) who will decide which service discovery backend to use. Libraries should never change the service discovery implementation as that is something owned by the application.

SwiftServiceDiscovery only provides the service discovery API. As an application owner, you need to select a service discovery backend to make querying available.

Selecting a backend is done by adding a dependency on the desired backend implementation and instantiating it at the beginning of the program.

For example, suppose you have chosen the hypothetical

as the backend:
// 1) Import the service discovery backend package
import DNSBasedServiceDiscovery

// 2) Create a concrete ServiceDiscovery object let serviceDiscovery = DNSBasedServiceDiscovery()

As the API has just launched, not many implementations exist yet. If you are interested in implementing one see the "Implementing a service discovery backend" section below explaining how to do so. List of existing SwiftServiceDiscovery API compatible libraries:

Obtaining a service's instances

To fetch the current list of instances (where

serviceDiscovery.lookup(service) { result in

To fetch the current list of instances (where

) AND subscribe to future changes:
let cancellationToken = serviceDiscovery.subscribe(
    to: service, 
    onNext: { result in
        // This closure gets invoked once at the beginning and subsequently each time a change occurs
    onComplete: { reason in
        // This closure gets invoked when the subscription completes


// Cancel the subscribe request cancellationToken.cancel()

returns a
that you can use to cancel the subscription later on.
is a closure that gets invoked when the subscription ends (e.g., when the service discovery instance shuts down) or gets cancelled through the
can be used to distinguish what leads to the completion.


SwiftServiceDiscovery includes combinators for common requirements such as transforming and filtering instances. For example:

// Only include instances running on port 8080
let serviceDiscovery = InMemoryServiceDiscovery(configuration: configuration)
    .filterInstance { [8080].contains($0.port) }

Implementing a service discovery backend

Note: Unless you need to implement a custom service discovery backend, everything in this section is likely not relevant, so please feel free to skip.

Adding the dependency

To add a dependency on the API package, you need to declare it in your

.package(url: "", from: "0.1.0"),

and to your library target, add "ServiceDiscovery" to your dependencies:

    name: "MyServiceDiscovery", 
    dependencies: [
        .product(name: "ServiceDiscovery", package: "swift-service-discovery"),

To become a compatible service discovery backend that all SwiftServiceDiscovery consumers can use, you need to implement a type that conforms to the

protocol provided by SwiftServiceDiscovery. It includes two methods,


/// Performs a lookup for the given service's instances. The result will be sent to `callback`.
/// `defaultLookupTimeout` will be used to compute `deadline` in case one is not specified.
/// - Parameters:
///   - service: The service to lookup
///   - deadline: Lookup is considered to have timed out if it does not complete by this time
///   - callback: The closure to receive lookup result
func lookup(_ service: Service, deadline: DispatchTime?, callback: @escaping (Result) -> Void)

fetches the current list of instances for the given service and sends it to
. If the service is unknown (e.g., registration is required but it has not been done for the service), then the result should be a

The backend implementation should impose a deadline on when the operation will complete.

should be respected if given, otherwise one should be computed using


/// Subscribes to receive a service's instances whenever they change.
/// The service's current list of instances will be sent to `nextResultHandler` when this method is first called. Subsequently,
/// `nextResultHandler` will only be invoked when the `service`'s instances change.
/// ### Threading
/// `nextResultHandler` and `completionHandler` may be invoked on arbitrary threads, as determined by implementation.
/// - Parameters:
///   - service: The service to subscribe to
///   - nextResultHandler: The closure to receive update result
///   - completionHandler: The closure to invoke when the subscription completes (e.g., when the `ServiceDiscovery` instance exits, etc.),
///                 including cancellation requested through `CancellationToken`.
/// -  Returns: A `CancellationToken` instance that can be used to cancel the subscription in the future.
func subscribe(to service: Service, onNext nextResultHandler: @escaping (Result) -> Void, onComplete completionHandler: @escaping (CompletionReason) -> Void) -> CancellationToken

"pushes" service instances to the
. The backend implementation is expected to call
  • When
    is first invoked, the caller should receive the current list of instances for the given service. This is essentially the
  • Whenever the given service's list of instances changes. The backend implementation has full control over how and when its service records get updated, but it must notify
    when the instances list becomes different from the previous result.

A new

must be created for each
request. If the cancellation token's
, the subscription has been cancelled and the backend implementation should cease calling the corresponding

The backend implementation must also notify via

when the subscription ends for any reason (e.g., the service discovery instance is shutting down or cancellation is requested through
), so that the subscriber can submit another
request if needed.

Do not hesitate to get in touch, over on

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.