FluuxIO / XMPP

Pure Swift XMPP library
https://www.process-one.net/
Apache License 2.0
44 stars 6 forks source link
chat chat-sdk swift xmpp xmpp-client

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:

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:

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

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.