ChatEngine class
The chat state machine: bootstrap → connect → ready → frames, plus every host-facing action. Pure Dart; the Flutter layer wires storage, the socket, lifecycle and widgets around it.
Constructors
- ChatEngine({required WidgetApi api, required SocketConnector connector, required SessionStore store, required HatangAppInfo appInfo, HatangLogger? logger, String? preferredLocale, Backoff? backoff, Duration backgroundCloseDelay = const Duration(seconds: 10), Duration typingIdle = const Duration(seconds: 3), Duration bootstrapRetry = const Duration(seconds: 5), bool openOnProactiveMessage = true})
- Creates an engine. Nothing happens until start.
Properties
- backgroundCloseDelay → Duration
-
How long after going to the background the socket is closed.
final
- bootstrapRetry → Duration
-
Delay before a failed bootstrap is retried (transient errors only).
final
-
events
→ Stream<
HatangEvent> -
Host-facing events.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- installId → String
-
The install id (after start).
no setter
- isStarted → bool
-
start was called and stop was not.
no setter
- openOnProactiveMessage → bool
-
Open the chat when the team starts a conversation with
open: true.final -
openRequests
→ Stream<
String> -
Conversation ids the team opened proactively with
open: true; the Flutter layer opens the screen.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → ChatState
-
Current snapshot.
no setter
-
states
→ Stream<
ChatState> -
Every snapshot change.
no setter
- token → String?
-
The current visitor token (after bootstrap).
no setter
- typingIdle → Duration
-
Idle time after the last keystroke before
typing off.final
Methods
-
attach(
UploadSource source, {String caption = ''}) → Future< void> -
Sends a file, optionally with a
caption. It shows at once as a pending bubble with upload progress; when the bytes are on the server the message goes out like text (same retry by client id). Files over 10 MB or of a type the gateway refuses fail immediately with a transient error. -
cancelEmailForm(
) → void - Dismisses the pre-chat form without sending.
-
clearPushToken(
) → Future< void> - Unregisters the push token.
-
clearTransientError(
) → void - Clears the error banner.
-
connect(
) → Future< void> - Opens the socket again after stop or a background close (bootstraps first when it never ran).
-
dispose(
) → void - Releases every resource.
-
identify(
HatangUser user) → Future< void> - Attaches an identity. Resolves once the gateway acknowledged it (or, on a merge, once the new session bootstrapped).
-
listConversations(
{String status = 'all', String? after, int limit = 20}) → Future< ConversationPage> -
The visitor's conversations, newest activity first, resolved ones
included (an in-app inbox).
statusisall,openorresolved; pass the last id of a page asafterfor the next one. -
loadEarlier(
) → Future< void> - Loads the page before the oldest loaded item.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onTextChanged(
String text) → void -
The composer text changed: sends one
typing on, thenoffafter typingIdle without keystrokes. -
openActive(
) → Future< void> - Back to the visitor's current conversation (the newest open one), or an empty chat when there is none, so the next message starts a new one.
-
openConversation(
String id, {VisitorConversationDto? summary}) → Future< void> -
Shows conversation
id: its latest 50 items, live updates while it is on screen, earlier ones through loadEarlier. Writing into a resolved conversation reopens it; a closed one is read-only.summary(from listConversations) avoids a flash of the wrong status. -
pushEvent(
String name, [Map< String, Object?> ? data]) → void - Records a custom event on the active conversation.
-
rate(
int score, {String? comment}) → void - Rates the conversation (CSAT).
-
refreshAttachments(
MessageDto message) → Future< void> - Signed attachment links expire after a day. When one fails to load, fetch that message again for fresh links (once per message per session).
-
reportPurchase(
{required String platform, required String storeProductId, String? transactionId, num? amount, String? currency, String? signedTransaction, String? purchaseToken, String? productType, String? kind}) → Future< bool> -
Reports a finished store purchase: with the store's proof
(
signedTransactionon iOS,purchaseToken+productTypeon Android) Hatang verifies it and records it for this person; the team also gets an internal note.falsewithout a session, on an older gateway, or when the store refuses it. -
reset(
) → Future< void> -
Forgets this visitor: the old token stops working, a fresh visitor is
bootstrapped and the push token (if any) is re-registered for it. Before
start (
autoConnect: false, chat never opened) the stored visitor is forgotten all the same, and the new one connects on the next start. -
resolvePaymentQr(
PaymentCard card) → Future< PaymentQrSource> -
Where to draw
card's VietQR code from (the card's payload, the public checkout's, or a raster image). ThrowsPaymentQrException. -
restoreSession(
String linkToken) → Future< void> - Swaps a magic-link token for that session.
-
retry(
String clientId) → Future< void> - Re-sends a failed message with the same client id (re-uploading its file first if the upload never finished).
-
send(
String text) → Future< void> -
Sends
text; shows the pre-chat form first when the team is away and the site wants an email. -
setData(
Map< String, Object?> data) → Future<void> - Attaches custom data.
-
setForeground(
bool foreground) → void - The app moved to the foreground or background.
-
setLocale(
HatangLocale locale) → Future< void> - Switches the UI language; the socket re-authenticates so the gateway learns it too.
-
setPushToken(
String token, {required PushProvider provider}) → Future< void> - Remembers the push token and registers it once a visitor token exists.
-
setScreen(
String name, {Map< String, String> ? params}) → void - Reports the screen the app shows.
-
setVisible(
bool visible) → void - The conversation is (not) on screen. Visible clears unread and sends a read receipt for everything loaded.
-
showLocalMessage(
String text, {String? senderName}) → void - Inserts a local-only bot bubble (never sent, never persisted).
-
skipEmail(
) → Future< void> - The visitor declined the optional email; the held-back draft is sent.
-
start(
) → Future< void> - Loads the stored session, bootstraps and connects.
-
stop(
) → Future< void> - Closes the socket and stops retrying; start may be called again.
-
submitEmail(
String email) → Future< void> - Inline email request answered: identifies the visitor by email.
-
submitOfflineForm(
{String? name, String? email, String? message}) → Future< void> - Offline pre-chat form: posts the first message with name and email.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited