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 Chat server use the code snippet below.
String login = "chrispeterson";
String password = "superPassword";
try {
await QB.chat.connect(login, password);
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
Subscribe to connection state
Subscribe to the connection state changes using the following code snippet.
//Chat Connections
//QBChatEvents.CONNECTED
//QBChatEvents.CONNECTION_CLOSED
//QBChatEvents.RECONNECTION_FAILED
//QBChatEvents.RECONNECTION_SUCCESSFUL
String event = QBChatEvents.CONNECTED;
try {
connectedSubscription = await QB.chat.subscribeChatEvent(event, (data) {
}
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
Check if connected to Chat server
Check the connection state using the following code snippet.
try {
bool? connected = await QB.chat.isConnected();
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
Disconnect from Chat server
Disconnect from the Chat server using the snippet below.
try {
await QB.chat.disconnect();
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
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.
try {
await QB.settings.enableAutoReconnect(true);
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
Manage Chat connections
To provide a seamless chat experience, our SDK manages connections to the Chat server at an application-wide level. Thus, to handle offline messages correctly, use the disconnect()
method when an app goes to the background and the connect()
method when an app goes to the foreground. As a result, the SDK will disconnect/connect the current user from the Chat server when the app goes to the background/foreground mode.
class _SomeScreenState extends State<SomeScreen> with WidgetsBindingObserver {
//...
@override
initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
try {
await QB.chat.connect(userId, userPassword);
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
break;
case AppLifecycleState.paused:
print("app in paused");
try {
await QB.chat.disconnect();
} on PlatformException catch (e) {
// Some error occurred, look at the exception message for more details
}
break;
}
}
}
//...
Argument | Required | Description |
---|---|---|
userId | yes | The ID of a user. |
userPassword | yes | The password of the user. |
Updated over 3 years ago