# Whixp: A Minimal Working Example (in Dart)

## Introduction

This document provides a minimal working example of a client implementation using the Whixp library, making it connect to a running Openfire server.

Topics that are covered in this document:

## Background

Whixp is an XMPP Client Library for Dart and Flutter

This guide describes how to use Whixp to connect to Openfire. It provides nothing more than a minimal working example, intended as a stepping stone to for client developers that get started with a new project.

## Preparations

In this example, a client connection will be made against a running Openfire server. For ease of configuration, the 'demoboot' setup of Openfire is used.

The 'demoboot' setup of Openfire allows one to start a fresh installation of Openfire into a certain provisioned state, without running any of the setup steps. When running in 'demoboot' mode:

- an administrative account is created using the username 'admin' and password 'admin'
- three users are automatically created: 'jane', 'john' and 'juan' (all using the value 'secret' as their password)
- the XMPP domain name is configured to be 'example.org' (for ease of use, configure 'example.org' to be an alias of '127.0.0.1' in your hosts file!)

To start Openfire in 'demoboot' mode, you can invoke the Openfire executable using the `-demoboot` argument, as shown below.

Starting Openfire in 'demoboot' mode.

    $ ./bin/openfire.sh -demoboot

That should be everything that you need to get Openfire running. Background information on the 'demoboot' mode can be found in Openfire's Demoboot Guide.

## Code

To start the project, create a file named `pubspec.yaml` in an empty directory, and copy in the code below.

Example pubspec.yaml file

    name: example
    description: A Whixp sample for Openfire.
    version: 1.0.0

    environment:
      sdk: ^3.4.0

    dependencies:
      whixp: ^2.1.2+1

Next, create a file named `main.dart` in an empty directory, and copy in the code below.

Example main.dart file

    import 'package:whixp/whixp.dart';

    void main() {
      final whixp = Whixp(
        jabberID: 'john@example.org/my-resource',
        password: 'secret',
        port: 5223,
        useTLS: true,
        onBadCertificateCallback: (_) => true,
        logger: Log(enableWarning: true, enableError: true, includeTimestamp: true),
        internalDatabasePath: 'whixp',
        reconnectionPolicy: RandomBackoffReconnectionPolicy(1, 3),
      );

      /// Reconnect on disconnection.
      whixp.addEventHandler('state', (state) {
        if (state == TransportState.disconnected) whixp.connect();
      });
      whixp.connect();
    }

Finally, build and run the test client, using the instructions below.

Build and run Whixp test client

    $ dart pub get
    $ dart run main.dart

If all goes well, this will print a short exchange of XMPP data! Press control-c to end the application.

Note that this example disables important security features. You should not use this for anything important!

## Further Reading

Please use the links below to find more information.

- Whixp documentation page
- Whixp pub/dev repository page