sendTypingIndicator method
Sends typing indicator
Implementation
void sendTypingIndicator(bool isTyping) {
final userId = ChatConfig.instance.userId;
if (!ChatConfig.instance.enableTypingIndicators ||
_socket == null ||
!_socket!.connected ||
userId == null ||
_receiverId.isEmpty) {
return;
}
_typingTimer?.cancel();
final now = DateTime.now();
if (isTyping) {
bool canSendTyping = _lastTypingIndicatorSent == null ||
now.difference(_lastTypingIndicatorSent!).inMilliseconds > 1000;
if (canSendTyping) {
_log('Sending typing indicator: typing');
_socket!.emit(_socketEvents.typingStartEvent,
{'userId': userId, 'receiverId': _receiverId});
_lastTypingIndicatorSent = now;
// Auto-stop after 3 seconds
_typingTimer = Timer(Duration(seconds: 3), () {
_socket!.emit(_socketEvents.typingEndEvent,
{'userId': userId, 'receiverId': _receiverId});
});
}
} else {
_log('Sending typing indicator: stopped');
_socket!.emit(_socketEvents.typingEndEvent,
{'userId': userId, 'receiverId': _receiverId});
_lastTypingIndicatorSent = null;
}
}