Connection
Learn how to connect to the chat server and set connection settings.
Before you begin
- 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.
- Configure QuickBlox SDK for your app. Check out our Setup page for more details.
- 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.
Connect to chat server with QuickBlox session token
In case, you authenticate with QuickBlox via Firebase, Facebook, or Custom Identity Provider, you should connect to the chat server with QuickBlox user session token and QuickBlox user ID.
As a result of successful authentication via Firebase, Facebook, or Custom Identity Provider, you receive a QuickBlox user session token and QuickBlox user ID. Use QuickBlox user session token as a password and QuickBlox user ID as a login to connect to the chat server.
Don’t use the Firebase/Facebook access token as a password to connect to the chat server.
You will receive the following runtime error in this case:
The login
method accepts a user
argument with the following fields:
Fields | Required | Description |
---|---|---|
id | yes | The ID of a user. |
token | yes | Specifies an active QuickBlox user session token set as a password. |
Subscribe to connection state
Subscribe to the connection state changes using the following code snippet.
Check if connected to chat server
Check the connection state using the following code snippet.
Disconnect from chat server
Disconnect from the chat server using the snippet below.
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.
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.
Make sure to add an instance of the created lifecycle observer class to the androidx.lifecycle.ProcessLifecycleOwner()
to process lifecycle changes within your app.
Set connection settings
Use the settings below to configure the connection to the chat server.
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. |
Was this page helpful?