# XMPP Setup Guide

The XMPP Chat SDK is a module that enables XMPP chat for the popular Chat SDK for iOS Messaging Framework.

The Chat SDK provides:

* User interface
* Data persistence

The XMPP module then provides a network adapter that allows the Chat SDK UI to communicate with an XMPP server.

### Documentation

Since the XMPP project uses the standard Chat SDK UI, there is a lot of documentation available on the Github project pages:

* [Android Github Project Page](https://github.com/chat-sdk/chat-sdk-android)
* [iOS Github Project Page](https://github.com/chat-sdk/chat-sdk-ios)
* [API Docs](https://github.com/chat-sdk/docs)
* [iOS Wiki](https://github.com/chat-sdk/chat-sdk-ios/wiki)
* [Setup for File transfer and push notifications](https://github.com/chat-sdk/chat-sdk-firebase)

### Server setup

The XMPP Chat SDK supports currently supports ejabberd and OpenFire. It may also support other XMPP servers but they have not been tested.

#### ejabberd

First got to the ejabberd website and download the [Community Version](https://www.process-one.net/en/ejabberd/#getejabberd).

Follow the instructions on the ejabberd site to install ejabberd on your server.

Once the server is setup, you need to change some settings:

**Enable registration**

Change the ejabberd configuration `conf/ejabberd.yml`

```
trusted_network:
    allow: all

local: 
    all: allow

mod_vcard: 
    search: true
    matches: 10
    allow_return_all: true
```

Start ejabberd by going to the installation directory on your server and running:

```
sudo sh bin/ejabberdctl start
```

You can check that ejabberd is up and running by logging in to the server admin:

```
http://your-server.com:5280/admin
```

#### OpenFire

Install [OpenFire](https://www.igniterealtime.org/projects/openfire/) on your server or use a pre-installed OpenFire hosting service like the one offered by [1and1](https://www.1and1.com/cloud-app-center/openfire-download?ac=OM.UK.UKf11K357009T7073a\&kwk=13287984).

Once you've done that, go into the admin panel and enable in-band registration.

Then enable the search plugin.

#### Prosody

Make sure you have the following modules enabled:

1. lastactivity
2. disco
3. muc
4. vcard
5. blocklist
6. carbons
7. time
8. smacks

### Client setup

The XMPP Chat SDK zip file contains everything you need to compile and run the project. However, a few steps are needed before you can compile the project.

#### iOS Setup

Use the terminal to navigate to the `XcodeXMPPSwift` folder. Then run `pod update`. This will install the project using Cocoapods.

Now open your app delegate and add the following code to setup the Chat SDK:

```
let config = BConfiguration.init();
config.googleMapsApiKey = "Google Maps Key"
config.loginUsernamePlaceholder = "JID"

// Set to true for non-https connections
config.xmppUseHTTP = true
config.logoImage = UIImage(named: "logo")
config.clientPushEnabled = true

// Set the domain and host address of the server
config.xmpp(withDomain: "domain", hostAddress: "host", port: 5222, resource: "iphone")

BChatSDK.initialize(config, app: application, options: launchOptions)
```

#### Android Setup

In your main application class setup the Chat SDK then configure the XMPP server:

```
ChatSDK.builder()

        // Configure the library
        .setGoogleMaps("Your Google Maps Key")
        .setAnonymousLoginEnabled(false)
        .setDebugModeEnabled(false)
        .build()

        // Add modules to handle file uploads, push notifications
        .addModule(FirebaseUploadModule.shared())
        .addModule(FirebasePushModule.shared())

        .addModule(XMPPModule.builder()
                .setXMPP("host", "domain")
                .setAllowServerConfiguration(false)
                .setPingInterval(5).build())
        .addModule(UIModule.builder()
                .setMessageSelectionEnabled(false)
                .setUsernameHint("JID")
                .setResetPasswordEnabled(false)
                .build())
        .addModule(LocationMessageModule.shared())

        .build().activate(this, "your-email-address");
```

Update the Android SDK and NDK locations in `local.properties`

```
ndk.dir=/Users/Shared/Projects2/Libs/NDK
sdk.dir=/Users/Ben3/Library/Android/sdk
```

Then run the `app-xmpp` target.

#### Google Maps

Follow the instructions [here](https://hackmd.io/@dyR2Vn0UTFaO8tZjyiJyHw/rkyHX76hU).

**iOS**

```
config.googleMapsApiKey = "Google Maps Key"
```

#### Push Notifications

By default, push notifications are handled by Firebase Cloud Messaging. For this to work, you will need to create a Firebase account, link the project to your Firebase account and then perform some configuration steps.

**iOS**

Go to [this link](https://github.com/chat-sdk/chat-sdk-ios#firebase-setup) and follow steps 1 - 7.

**Android**

Go to [this link](https://hackmd.io/@dyR2Vn0UTFaO8tZjyiJyHw/BkvpPKFqI) and follow steps: 1 - 7.

**Then**

Go to [this link](https://github.com/chat-sdk/chat-sdk-firebase#enable-push-notifications) and follow steps 1 - 21.

#### File Upload

By default, the project uses Firebase Cloud Storage to store uploaded files. To do this follow the steps under **Deploy the storage rules** - [here](https://github.com/chat-sdk/chat-sdk-firebase#deploy-the-storage-rules).

### Support

* **Discord:** If you need support, join our [Server](https://discord.gg/abT5BM4)
* **Email:** <team@sdk.chat>
