Before you begin
- Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
- Configure QuickBlox SDK for your app. Check out Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See Authentication page to learn how to do it.
- Connect to the Chat server. See Connection page to learn how to do it.
- Create a dialog. See Dialogs page to learn how to do it.
Subscribe message events
Add the event delegate to receive messages in real-time using theaddDelegate() method. The event delegate enables the app to listen to message events associated with receiving a message, delivery receipts, and read receipts.
- Swift
- Objective-C
QBChatDelegate methods in your chat controller to track events in your chat.
- Swift
- Objective-C
Send text message
To send a message to a private dialog, use the code snippet below.- Swift
- Objective-C
- Swift
- Objective-C
Send message with attachment
Chat attachments are supported by the content API. In order to send a chat attachment, you need to upload the file to QuickBlox cloud storage and obtain a link to the file (file UID). Then you need to include this UID into the chat message and send it.- Swift
- Objective-C
- Swift
- Objective-C
Send message with extra data
You have an option to extend the message with additional fields. Specify one or more key-value items in themessage. Using these items, you can implement the ability for a user to send self-location information to another user or notification messages signifying that a user has left a group, etc.
- Swift
- Objective-C
Set the following fields of the
message:
Retrieve chat history
Every dialog stores its chat history that you can retrieve using themessages(withDialogID:extendedRequest:) method. The request below will return messages for a specific dialog, sorted by the date_sent field in descending order, limited to 50 dialogs on the page.
- Swift Concurrency
- Swift
- Objective-C
extendedRequest dictionary.
If you want to get a paginated list of messages from the server, you can set the key-value parameters in the extendedRequest dictionary.
Search operators
You can use search operators to get more specific search results.Sort operators
You can use sort operators to order the search results.Update message
Update the message text using theupdateMessage(withID:text:dialogID:) method below.
- Swift
- Objective-C
Delete message
Any user in theoccupantIDs can delete a message from the dialog. As a result, the message will be deleted from the current user history, without affecting the histories of other users.
The owner of the dialog can completely remove messages from all users’ histories. This is achieved by setting the forAllUsers parameter to true.
- Swift
- Objective-C
Check if a message is sent
The message is considered as sent if it has been delivered to the server. To get to know that a message has been delivered to the server, make sure to enable a stream management before connecting to the Chat server. See this section to learn how to enable the stream management. Thus, you send a message to the server and if no error is returned, it is considered as sent (by default). There is no field for a sent status in the message model.- Swift
- Objective-C
Mark message as delivered
As a sender, you may want to be informed that a message has been successfully delivered to the recipient. The mark-as-delivered functionality allows to notify the sender about message delivery. To track the event when the message has been delivered to the user, use thechatDidDeliverMessage(messageID:dialogID:) method of the QBChatDelegate delegate.
- Swift
- Objective-C
mark(asDelivered:) method to mark a message as delivered. As a result, the server will notify a sender about the delivery receipt.
- Swift
- Objective-C
A message can be marked as delivered automatically by the server once a message is successfully delivered to the recipient. Set the
markable as true using the send() method if you want, as a sender, to receive message delivery receipts from other recipients. Thus, the markable parameter enables the sender to request the delivery receipt. It also enables the recipient to confirm the message delivery. However, if markable is false or omitted, then you can notify a sender about the delivery receipt using the mark(asDelivered:) method.
- Swift
- Objective-C
delivered_ids field in the message model, on the server, by using the method below. As a result, the ID of the user who has received the message will be added to the array of delivered_ids field. However, the server won’t notify the user about the change in the message model.
- Swift
- Objective-C
Mark message as read
As a sender, you may want to be informed that a message has been read by the recipient. The mark-as-read functionality allows to notify the sender that a message has been read. To track the event when the message has been read by the user, thechatDidReadMessage(messageID:dialogID:readerID:) method of the QBChatDelegate is used.
- Swift
- Objective-C
read() method to mark a message as read. As a result, the server will notify a sender about the read receipt.
- Swift
- Objective-C
When a message is marked as read, the IDs of users who have read the message are stored in the message model, on the server. Thus, you can request a chat history from the server to get to know who read the message using the
messages(withDialogID:) method. See this section to learn how to retrieve chat history.read_ids field in the message model, on the server, by using the method below. As a result, the ID of the user who has read the message will be added to the array of read_ids field. However, the server won’t notify the user about the change in the message model.
- Swift
- Objective-C
Send typing indicators
You may want, as a sender, to let the recipient know that you are typing the message or have stopped typing the message. Use typing indicators as a form of chat-specific presence. Typing indicators allow to indicate if users are typing messages in a dialog at the moment. There are the following typing notifications supported.- typing. The user is composing a message. The user is actively interacting with a message input interface specific to this chat session (for example, by typing in the input area of a chat window).
- stopped. The user had been composing but now has stopped. The user has been composing but has not interacted with the message input interface for a short period of time (for example, 30 seconds).
onUserIsTyping(userID:) method of the delegate.
- Swift
- Objective-C
onUserStoppedTyping(userID:) method of the delegate.
- Swift
- Objective-C
sendUserIsTyping() method. As a result, the server will notify a recipient about the event.
- Swift
- Objective-C
sendUserStopTyping() method. As a result, the server will notify a recipient about the event.
- Swift
- Objective-C
Send system messages
There is a way to send system messages to other users about some events. For example, a system message can be sent when a user has joined or left a group dialog. System messages are handled over a separate channel and are not mixed up with regular chat messages. Thus, they are handled bychatDidReceiveSystemMessage() callback of QBChatDelegate. See this section to learn how to add QBChatDelegate.
System messages are also not shown in the dialog history and, consequently, are not stored on the server. This means that these messages will be delivered only to online users. Send system messages using the sendSystemMessage() method.
- Swift
- Objective-C
Set the following fields of the
message: