Skip to content

FluuxIO/XMPP

Repository files navigation

Fluux XMPP

Fluux XMPP is a Chat SDK in Swift for iOS, MacOS and Linux. It implements the XMPP protocol, which is an IETF standard.

Fluux XMPP is a clean slate implementation, with the following goals in mind:

  • Focus on simplicity, by hiding XMPP technical details and exposing only chat oriented methods.
  • Focus on modern Swift, recent iOS version and modern XMPP. The library ignores legacy or deprecated features on purpose.
  • Focus on maintainability. XMPP is a complex protocol and maintainability should be the main criteria for design decision to be able to be sustainable in the long term. For the same reason, we also aim at limiting the dependencies as much as possible.
  • Focus on efficiency and scalability, leveraging only part of the XMPP protocol that are scalable. Many XEPs are too consuming to be used on large scale on production.

As a result, on iOS, we target iOS version 12+. On MacOS, we focus on Mojave and up (10.14+). On Linux, we use BSD Socket with SwiftNIO, so it should work quite broadly, as long as you have Swift 5.0+ installed.

Note: This library is under very active development and not yet ready for production.

Building Fluux XMPP library

We design the build system with the following principles:

  • The goal is to be multiplatform (iOS, MacOS, Linux), while making use of newest iOS and MacOS features.
  • We want to be flexible and easy to integrate with all major dependency management tools (SwiftPM, CocoaPods, Carthage). SwiftPM is however our primary target.

As such, we build the platform differently based on the target / build system:

  • With Carthage and Cocoapods and SwiftPM on MacOS, we use Apple Network.framework and thus target iOS 12+ and MacOS 10.14+. It means that for standard iOS ou MacOS build, there is no other dependencies.
    • With SwiftPM on Linux, we use Swift-NIO, as Network.framework is only available for Apple platforms.

This decision allows us to keep all build system very simple, while having a good platform reach.

Using Swift Package

Install homebrew dependencies

You need a libxml2 and a TLS implementation (i.e. libressl). You can install them with HomeBrew:

brew install libxml2
brew install libressl

Adding Fluux XMPP as a dependency in your project

You can build the lib using Swift Package Manager, thanks to the provided Package.swift file.

You also need to properly set your build target to MacOS 10.12 explicitely, as, at the moment, Swift PM uses MacOS 10.10 as an hardcoded value for the deployment target (more on this here).

To integrate Fluux XMPP in your Swift PM project, you can add it as a dependency of your project in your Package.swift file. For example:

// swift-tools-version:5.0

import PackageDescription

let package = Package(
    name: "XMPPDemo",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/FluuxIO/XMPP.git", from: "0.0.2"),
    ],
    targets: [
        .target(
            name: "XMPPDemo",
            dependencies: ["XMPP"]),
        .testTarget(
            name: "XMPPDemoTests",
            dependencies: ["XMPPDemo"]),
    ]
)

You can modify your command-line executable main.swift to start an XMPP client. For example:

import Foundation
import XMPP


guard let jid = JID("mremond@localhost/XMPPDemo") else { print("Invalid JID"); exit(1) }
var xmppConfig = Config(jid: jid, password: "mypass", useTLS: true)
xmppConfig.allowInsecure = true
xmppConfig.host = "MacBook-Pro-de-Mickael.local"
xmppConfig.streamObserver = DefaultStreamObserver()

let client = XMPP(config: xmppConfig)

let semaphore = DispatchSemaphore(value: 0)
client.connect {
  print("Disconnected !")
  semaphore.signal() 
}

_ = semaphore.wait(timeout: DispatchTime.distantFuture)

To build the project, you can just use the standard build command:

swift build

You can then run your console client:

.build/debug/XMPPDemo

The tests can be run with the command:

swift test

Working on Linux with Docker

You can use Docker official image to work on the Swift projet on Linux. You can also use Docker on MacOs to build the Linux version.

You can retrieve the Swift Docker image with the following command:

docker pull swift

You can then build open a Linux shell inside the container:

docker run  -itv $(pwd):/code --name swiftcode -w /code swift /bin/bash

You will then need to install libxml2-dev and libssl-dev dev packages in the container with:

apt-get update
apt-get install libxml2-dev libssl-dev

From the Docker shell, you can build the code with:

swift build

To run the test on Linux, you need to explicitely enable test discovery (See Swift Test Discovery):

swift test --enable-test-discovery

TLS support

At the moment the library only support standard TLS connection, not STARTTLS. Apple Networking library does not support (yet ?) switching encryption after the connection has been established, because they are worried by possible security issues around STARTTLS implementations.

That said, XMPP support TLS on port 5223. It is called "legacy" SSL because use of port 5223 has been deprecated by the XMPP Standards Foundation a while back. That said, modern XMPP servers like ejabberd support state of the art TLS on port 5223. To use TLS at the moment, it is perfectly fine to use TLS on port 5223.