QuickBlox Documentation

QuickBlox provides powerful Chat API and SDK to add real-time messaging and video calls to your web and mobile apps. Learn how to integrate QuickBlox across multiple platforms.

Connection

Learn how to connect to the chat server and set connection settings.

Before you begin

  1. Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
  2. Configure QuickBlox SDK for your app. Check out our Setup page for more details.
  3. Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.

Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.

Connect to Chat server

To connect to the Chat server use the code snippet below.

QBChatService.getInstance().login(qbUser, new QBEntityCallback() {
    @Override
    public void onSuccess(Void o, Bundle bundle) {
 
    }

    @Override
    public void onError(QBResponseException e) {

    }
});
QBChatService.getInstance().login(qbUser, object : QBEntityCallback<Void> {
    override fun onSuccess(o: Void?, bundle: Bundle?) {

    }

    override fun onError(e: QBResponseException?) {

    }

Subscribe to connection state

Subscribe to the connection state changes using the following code snippet.

ConnectionListener connectionListener = new ConnectionListener() {
    @Override
    public void connected(XMPPConnection xmppConnection) {

    }

    @Override
    public void authenticated(XMPPConnection xmppConnection, boolean b) {

    }

    @Override
    public void connectionClosed() {

    }

    @Override
    public void connectionClosedOnError(Exception e) {

    }

    @Override
    public void reconnectionSuccessful() {

    }

    @Override
    public void reconnectingIn(int i) {

    }

    @Override
    public void reconnectionFailed(Exception e) {

    }
};

QBChatService.getInstance().addConnectionListener(connectionListener);
val connectionListener = object : ConnectionListener {
    override fun connected(xmppConnection: XMPPConnection) {

    }

    override fun authenticated(xmppConnection: XMPPConnection, b: Boolean) {

    }

    override fun connectionClosed() {

    }

    override fun connectionClosedOnError(e: Exception) {

    }

    override fun reconnectionSuccessful() {

    }

    override fun reconnectingIn(i: Int) {

    }

    override fun reconnectionFailed(e: Exception) {

    }
}

QBChatService.getInstance().addConnectionListener(connectionListener)

Check if connected to Chat server

Check the connection state using the following code snippet.

boolean isLoggedIn = chatService.isLoggedIn();
val isLoggedIn = chatService.isLoggedIn

Disconnect from Chat server

Disconnect from the Chat server using the snippet below.

final QBChatService chatService = QBChatService.getInstance();
boolean isLoggedIn = chatService.isLoggedIn();
if (!isLoggedIn) {
    return;
}

chatService.logout(new QBEntityCallback<Void>() {
    @Override
    public void onSuccess(Void aVoid, Bundle bundle) {
        chatService.destroy();
    }

    @Override
    public void onError(QBResponseException e) {

    }
});
val chatService = QBChatService.getInstance()
val isLoggedIn = chatService.isLoggedIn
if (!isLoggedIn) {
    return
}

chatService.logout(object : QBEntityCallback<Void> {
    override fun onSuccess(aVoid: Void?, bundle: Bundle?) {
        chatService.destroy()
    }

    override fun onError(e: QBResponseException?) {

    }
})

Enable auto-reconnect to Chat

The SDK reconnects automatically when the connection to the Chat server is lost. There is a way to disable it and then manage it manually.

QBChatService.getInstance().setReconnectionAllowed(true);
QBChatService.getInstance().isReconnectionAllowed = true

Manage Chat connections

To provide a seamless chat experience, our SDK manages connections to the Chat server at an application-wide level. Thus, when your application goes background, you should disconnect from the Chat server to be able to receive offline messages. When your application goes foreground, you should connect to the Chat server to become online and start sending and receiving messages.

Use androidx.lifecycle.LifecycleObserver to determine when the application goes background.

public class BackgroundListener implements LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    void onBackground() {
        QBChatService.getInstance().logout(new QBEntityCallback<Void>() {
            @Override
            public void onSuccess(Void aVoid, Bundle bundle) {
                QBChatService.getInstance().destroy();
            }

            @Override
            public void onError(QBResponseException e) {

            }
        });
    }
}
class BackgroundListener : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    internal fun onBackground() {
        QBChatService.getInstance().logout(object : QBEntityCallback<Void> {
            override fun onSuccess(v: Void?, b: Bundle?) {
                QBChatService.getInstance().destroy()
            }

            override fun onError(e: QBResponseException?) {
                
            }
        })
    }
}

Make sure to add an instance of the created lifecycle observer class to the androidx.lifecycle.ProcessLifecycleOwner() to process lifecycle changes within your app.

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(new BackgroundListener());
    } 
}
class App : Application() {

    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(BackgroundListener())
    }
}

Set connection settings

Use the settings below to configure the connection to the Chat server.

// Chat connection configuration
QBChatService.ConfigurationBuilder configurationBuilder = new QBChatService.ConfigurationBuilder();
configurationBuilder.setSocketTimeout(300);
configurationBuilder.setUseTls(true);
configurationBuilder.setKeepAlive(true);
configurationBuilder.setAutojoinEnabled(false);
configurationBuilder.setAutoMarkDelivered(true);
configurationBuilder.setAllowListenNetwork(true);
configurationBuilder.setPort(5223);

QBChatService.setConfigurationBuilder(configurationBuilder);

QBChatService chatService = QBChatService.getInstance();

chatService.login(qbUser, new QBEntityCallback() {
    @Override
    public void onSuccess(Object o, Bundle bundle) {

    }

    @Override
    public void onError(QBResponseException e) {

    }
});
// Chat connection configuration
val configurationBuilder = QBChatService.ConfigurationBuilder()
configurationBuilder.socketTimeout = 300
configurationBuilder.isUseTls = true
configurationBuilder.isKeepAlive = true
configurationBuilder.isAutojoinEnabled = false
configurationBuilder.setAutoMarkDelivered(true)
configurationBuilder.isReconnectionAllowed = true
configurationBuilder.setAllowListenNetwork(true)
configurationBuilder.port = 5223

QBChatService.setConfigurationBuilder(configurationBuilder)

val chatService = QBChatService.getInstance()

chatService.login(qbUser, object : QBEntityCallback<Void> {
    override fun onSuccess(o: Void?, bundle: Bundle?) {

    }

    override fun onError(e: QBResponseException?) {

    }
})

Parameters

Description

setSocketTimeout

Chat socket read timeout in seconds. Default: 60.

setUseTls

TLS security mode used when making the connection.
Default: true .

setKeepAlive

Keep-alive option for a socket connection. Keep-alive is the option allowing to detect a stale connection. Default: true.

setAutojoinEnabled

Automatically join dialogs loaded or created on the server. Default: false.

setAutoMarkDelivered

Automatically mark the received messages as delivered.
Default: true .

setAllowListenNetwork

Allow SDK to listen to changes of network states. Default: true.

setPort

Chat connection port number.
Default: 5223.

Updated 5 months ago


What's Next

Dialogs

Connection


Learn how to connect to the chat server and set connection settings.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.