openConnection method
Creates a new WebSocket connection with the current user.
If includeUserDetailsInConnectCall
is true it will include the current
user details in the connect call.
Implementation
Future<OwnUser> openConnection({
bool includeUserDetailsInConnectCall = false,
}) async {
assert(
state.currentUser != null,
'User is not set on client, '
'use `connectUser` or `connectAnonymousUser` instead',
);
final user = state.currentUser!;
logger.info('Opening web-socket connection for ${user.id}');
if (wsConnectionStatus == ConnectionStatus.connecting) {
throw StreamChatError('Connection already in progress for ${user.id}');
}
if (wsConnectionStatus == ConnectionStatus.connected) {
throw StreamChatError('Connection already available for ${user.id}');
}
_wsConnectionStatus = ConnectionStatus.connecting;
// skipping `ws` seed connection status -> ConnectionStatus.disconnected
// otherwise `client.wsConnectionStatusStream` will emit in order
// 1. ConnectionStatus.disconnected -> client seed status
// 2. ConnectionStatus.connecting -> client connecting status
// 3. ConnectionStatus.disconnected -> ws seed status
_connectionStatusSubscription =
_ws.connectionStatusStream.skip(1).listen(_connectionStatusHandler);
try {
final event = await _ws.connect(
user,
includeUserDetails: includeUserDetailsInConnectCall,
);
return user.merge(event.me);
} catch (e, stk) {
logger.severe('error connecting ws', e, stk);
rethrow;
}
}