|
| 1 | +### Use Flutter-MQTT-Chat-Client as a library to your project |
| 2 | + |
| 3 | +## 1. Login |
| 4 | +```dart |
| 5 | +bool connected = await ChatApp.instance()!.clientHandler.connect( |
| 6 | + host: "broker.url.com", |
| 7 | + |
| 8 | + password: "user_pass"); |
| 9 | +``` |
| 10 | + |
| 11 | +## 2. Listen to my profile changes |
| 12 | +```dart |
| 13 | +ChatApp.instance()!.archiveHandler.getUser().listen((user) { |
| 14 | + //insert/update user to database |
| 15 | + }); |
| 16 | +``` |
| 17 | + |
| 18 | +## 3. Listen to rooms |
| 19 | +Whenever the logged in user is added to a room (or room details changed), the room details will be added to the Conversations stream. |
| 20 | +```dart |
| 21 | +ChatApp.instance()!.archiveHandler.getAllConversations().listen((rooms) { |
| 22 | + //insert/update rooms on the Database |
| 23 | + }); |
| 24 | +``` |
| 25 | + |
| 26 | +## 4. Listen to new messages |
| 27 | +```dart |
| 28 | +ChatApp.instance()!.messageReader.getChatMessages().listen((message) { |
| 29 | + var dbMessage = message.toDbMessage(); |
| 30 | + // insert the message to the database |
| 31 | + // send chatmarker if the message is not mine |
| 32 | + }); |
| 33 | +``` |
| 34 | + |
| 35 | +## 5. Listen to ChatMarkers |
| 36 | +```dart |
| 37 | +ChatApp.instance()! |
| 38 | + .messageReader |
| 39 | + .getChatMarkerMessages() |
| 40 | + .listen((markerMessage) { |
| 41 | +
|
| 42 | + String messageId = markerMessage.referenceId; |
| 43 | + if (markerMessage.status == ChatMarker.displayed) { |
| 44 | + //update the database record |
| 45 | + } else if (markerMessage.status == ChatMarker.delivered) { |
| 46 | + //update the database record |
| 47 | + } |
| 48 | + }); |
| 49 | +``` |
| 50 | + |
| 51 | +## 6. Listen to new chat invitations |
| 52 | +```dart |
| 53 | +ChatApp.instance()! |
| 54 | + .invitationHandler |
| 55 | + .newInvitationsStream() |
| 56 | + .listen((invitation) { |
| 57 | + if(invitation.type == MessageType.EventInvitationRequest) { |
| 58 | + //new invitation request |
| 59 | + //insert invitation record to the database, notify the user |
| 60 | + } |
| 61 | + if(invitation.type == MessageType.EventInvitationResponseAccept || invitation.type == MessageType.EventInvitationResponseReject) { |
| 62 | + //responded to invitation, update the local record and wait the server to sync the new contact (if accepted). |
| 63 | + //Do not insert a room, the user will receive a new room details triggered by getAllConversations() |
| 64 | + } |
| 65 | + }); |
| 66 | +``` |
| 67 | + |
| 68 | +## 7. Listen to invitatioin updates |
| 69 | +We use this to listen to the invitations that the user has sent. It could be: |
| 70 | +- Info: When receive an affirmation that the invitation is sent |
| 71 | +- Error: When there is an error (like the user is not found) |
| 72 | +- |
| 73 | +```dart |
| 74 | +ChatApp.instance()! |
| 75 | + .invitationHandler |
| 76 | + .invitationUpdatesStream() |
| 77 | + .listen((invitation) { |
| 78 | + if (invitation.invitationMessageType == InvitationMessageType.INFO) { |
| 79 | + //update the invitation record to be confirmed |
| 80 | + } |
| 81 | + else if (invitation.invitationMessageType == InvitationMessageType.ERROR) { |
| 82 | + //notify the user, and delete the record in inserted |
| 83 | + } |
| 84 | + }); |
| 85 | +``` |
| 86 | + |
| 87 | +## 8. Send a text Message |
| 88 | +```dart |
| 89 | +ChatMessage newMessage = ChatMessage( |
| 90 | + id: "generated_random_id", |
| 91 | + type: MessageType.ChatText, |
| 92 | + text: "Hello there", |
| 93 | + roomId: "[room_id]", |
| 94 | + fromId: "[my_id]",//optional |
| 95 | + sendTime: DateTime.now().millisecondsSinceEpoch, |
| 96 | + fromName: "[my_name]"); |
| 97 | +``` |
| 98 | +### 8.1 Send the message as regular message |
| 99 | +```dart |
| 100 | +ChatApp.instance()! |
| 101 | + .messageSender |
| 102 | + .sendChatMessage(newMessage, "[room_id]"); |
| 103 | +``` |
| 104 | + |
| 105 | +### 8.2 Send the message as a reply to another message |
| 106 | +```dart |
| 107 | +ChatMessage replyToMessage = ...; //the message to reply to |
| 108 | +ChatApp.instance()! |
| 109 | + .messageSender |
| 110 | + .replyToMessage(replyToMessage, newMessage, widget.contactChat.roomId); |
| 111 | +``` |
| 112 | + |
| 113 | +## 9. Send File Message |
| 114 | +```dart |
| 115 | + ChatApp.instance()!.messageSender.sendFileChatMessage( |
| 116 | + type: MessageType.ChatImage,//for example |
| 117 | + fileLocalPath: path, |
| 118 | + room: "[room_id]"); |
| 119 | +``` |
| 120 | + |
| 121 | +## 10. Send typing indicator |
| 122 | +```dart |
| 123 | +ChatApp.instance()! |
| 124 | + .eventsSender |
| 125 | + .sendIsTyping(true, "[room_id]"); |
| 126 | +``` |
| 127 | + |
| 128 | +## 11. Listen to typing indicator |
| 129 | +```dart |
| 130 | +ChatApp.instance()!.messageReader.getTypingMessages().listen((event) { |
| 131 | + //using event.roomId and event.isTyping and event.fromId, update the ui state |
| 132 | + |
| 133 | + }); |
| 134 | +``` |
| 135 | + |
| 136 | +## 12. Send new chat invitation |
| 137 | +```dart |
| 138 | +ChatApp.instance()! |
| 139 | + .eventsSender |
| 140 | + .sendInvitation("[invitee_username]", "[invitation_random_id]"); |
| 141 | +``` |
0 commit comments