connect method
dynamic
connect(
- String apiKey
)
Implementation
connect(String apiKey) {
_channel = WebSocketChannel.connect(apiEndpoint);
logger.d(
"Connected to $apiEndpoint with apiKey: $apiKey and contact identifier: $contactIdentifier");
// send initial message to subscribe to the channel
_channel?.sink.add(
ChatwootSubscribeMessage(
command: "subscribe",
identifier: ChatwootIdentifier(
pubsubToken: apiKey,
channel: "RoomChannel",
),
).toString(),
);
// listen to the stream
_channel?.stream.listen((event) {
var message = chatwootWebsocketResponseFromJson(event);
logger.d("Received message: $event");
if (message.message?.data?.content != null ||
message.message?.data?.attachments != null) {
logger.d(
"Received message: ${message.message?.data?.content} from sender: ${message.message?.data?.sender?.id}");
if (message.message?.data?.sender?.id.toString() == userId) {
return;
}
_streamController.sink.add(message.toChatwootMessage());
}
}, onError: (error) {
logger.e("Error: $error");
// reconnect
connect(apiKey);
}, onDone: () {
logger.d("Done");
// reconnect
connect(apiKey);
});
}