diff --git a/content/chat/connect.textile b/content/chat/connect.textile index 3c71c7650b..1ececdad1f 100644 --- a/content/chat/connect.textile +++ b/content/chat/connect.textile @@ -5,6 +5,7 @@ product: chat languages: - javascript - react + - swift --- When you "instantiate":/chat/setup#instantiate a client, a realtime connection is established and maintained with Ably. You can interact with the connection using the @ChatClient.connection@ object in order to monitor a client's connection status. @@ -24,6 +25,9 @@ A connection can have any of the following statuses: blang[javascript]. Use the "@current@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.ConnectionStatus.html#current property to check which status a connection is currently in: +blang[swift]. + Use the "@status@":https://sdk.ably.com/builds/ably/#status property to check which status a connection is currently in: + blang[react]. Use the "@currentStatus@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_react.UseChatConnectionResponse.html#currentStatus property returned in the response of the "@useChatConnection@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/functions/chat_react.useChatConnection.html hook to check which status a connection is currently in: @@ -44,6 +48,10 @@ const MyComponent = () => { }; ``` +```[swift] +let status = await chatClient.connection.status +``` + blang[react]. Hooks related to chat features, such as @useMessages@ and @useTyping@, also return the current @connectionStatus@ in their response. @@ -63,12 +71,17 @@ blang[react]. blang[javascript]. +blang[swift]. + blang[javascript]. Use the "@connection.status.onChange()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.ConnectionStatus.html#onChange method to register a listener for status change updates: blang[react]. Listeners can also be registered to monitor the changes in connection status. Any hooks that take an optional listener to monitor their events, such as typing indicator events in the @useTyping@ hook, can also register a status change listener. Changing the value provided for a listener will cause the previously registered listener instance to stop receiving events. All messages will be received by exactly one listener. +blang[swift]. + Use the "@connection.onStatusChange(bufferingPolicy: .unbounded)@":https://sdk.ably.com/builds/ably/#onStatusChange method to create a subscription through which you can asynchronously iterate for the status updates: + ```[javascript] const { off } = chatClient.connection.status.onStatusChange((change) => console.log(change)); ``` @@ -86,6 +99,13 @@ const MyComponent = () => { }; ``` +```[swift] +let subscription = chatClient.connection.onStatusChange(bufferingPolicy: .unbounded) +for await statusChange in subscription { + print("Connection status changed to: \(statusChange.current)") +} +``` + blang[javascript]. To remove the connection status listener, call the @off()@ function returned in the @subscribe()@ response: @@ -101,6 +121,13 @@ blang[javascript]. blang[react]. +blang[swift]. + To stop the subscription from firing new status events, call its @finish()@ function: + + ```[swift] + subscription.finish() + ``` + h2(#discontinuity). Handle connection discontinuity If a client briefly loses connection to Ably, for example when driving through a tunnel, the SDK will attempt to recover the connection. If the disruption lasts for less than 2 minutes, then on reconnection the SDK will automatically reattach to any rooms and replay any missed messages. @@ -112,6 +139,9 @@ blang[javascript]. blang[react]. Any hooks that take an optional listener to monitor their events, such as typing indicator events in the @useTyping@ hook, can also register a listener to be notified of, and handle, periods of discontinuity. + +blang[swift]. + Each feature of the Chat SDK provides an @subscribeToDiscontinuities()@ method to assist with this. This enables you to create a subscription that will fire an event when discontinuity occurs in that feature so that you can handle it, as needed. As mentioned above, to stop your subscription from firing new events call its @finish()@ method. For example, for messages: @@ -136,6 +166,13 @@ const MyComponent = () => { }; ``` +```[swift] +let subscription = room.messages.subscribeToDiscontinuities() +for await error in subscription { + print("Recovering from the error: \(error)") +} +``` + blang[react]. blang[javascript]. diff --git a/content/chat/rooms/history.textile b/content/chat/rooms/history.textile index 9ebffa2401..df0170c052 100644 --- a/content/chat/rooms/history.textile +++ b/content/chat/rooms/history.textile @@ -5,6 +5,7 @@ product: chat languages: - javascript - react + - swift --- The history feature enables users to retrieve messages that have been previously sent in a room. Ably stores chat messages for 24 hours by default. You can extend this up to 30 days by "contacting us":https://forms.gle/SmCLNFoRrYmkbZSf8. @@ -50,13 +51,32 @@ const MyComponent = () => { }; ``` +```[swift] +let paginatedResult = try await room.messages.get(options: .init(orderBy: .newestFirst)) +print(paginatedResult.items) +if paginatedResult.hasNext { + let next = try await paginatedResult.next! + print(next.items) +} else { + print("End of messages") +} +``` + The following optional parameters can be passed when retrieving previously sent messages: -|_. Parameter |_. Description | -| start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. | -| end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. | -| direction | The direction in which to retrieve messages from; either @forwards@ or @backwards@. | -| limit | Maximum number of messages to be retrieved, up to 1,000. | +blang[javascript]. + |_. Parameter |_. Description | + | start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. | + | end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. | + | direction | The direction in which to retrieve messages from; either @forwards@ or @backwards@. | + | limit | Maximum number of messages to be retrieved, up to 1,000. | + +blang[swift]. + |_. Parameter |_. Description | + | start | Earliest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp equal to, or greater than, this value will be returned. | + | end | Latest time to retrieve messages from, as a unix timestamp in milliseconds. Messages with a timestamp less than this value will be returned. | + | orderBy | The direction in which to retrieve messages from; either @oldestFirst@ or @newestFirst@. | + | limit | Maximum number of messages to be retrieved, up to 1,000. | h2(#subscribe). Retrieve messages sent prior to subscribing @@ -68,6 +88,9 @@ blang[javascript]. blang[react]. Use the "@getPrevious()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_react.UseMessagesResponse.html#getPreviousMessages method available from the response of the @useMessages@ hook to only retrieve messages that were received before the listener subscribed to the room. As long as a defined value is provided for the listener, and there are no message discontinuities, @getPreviousMessages()@ will return messages from the same point across component renders. If the listener becomes undefined, the subscription to messages will be removed. If you subsequently redefine the listener then @getPreviousMessages()@ will return messages from the new point of subscription. +blang[swift]. + Use the @getPreviousMessages()@ method from your messages subscription to only retrieve messages that were received before the subscription was created: + ```[javascript] const { getPreviousMessages } = room.messages.subscribe(() => { console.log('New message received'); @@ -114,6 +137,18 @@ const MyComponent = () => { }; ``` +```[swift] +let messagesSubscription = try await room.messages.subscribe(bufferingPolicy: .unbounded) +let paginatedResult = try await messagesSubscription.getPreviousMessages(params: .init(limit: 50)) // `orderBy` here is ignored and always `newestFirst` +print(paginatedResult.items) +if paginatedResult.hasNext { + let next = try await paginatedResult.next! + print(next.items) +} else { + print("End of messages") +} +``` + The following parameters can be passed when retrieving previously sent messages: |_. Parameter |_. Description | diff --git a/content/chat/rooms/index.textile b/content/chat/rooms/index.textile index 438804e1b4..c56f8d8857 100644 --- a/content/chat/rooms/index.textile +++ b/content/chat/rooms/index.textile @@ -5,6 +5,7 @@ product: chat languages: - javascript - react + - swift --- Rooms are used to organize and logically separate your users and chat messages into 'rooms'. They are the entry object into using chat and provide access to all other chat features, such as messages, online status and typing indicators. A room can represent a 1:1 chat between an agent and a customer, a private message between two users in a chat application, a large group conversation, or the chat section of a livestream with thousands of users. @@ -53,6 +54,10 @@ const App = () => { }; ``` +```[swift] +let room = try await chatClient.rooms.get(roomID: "basketball-stream", options: RoomOptions()) +``` + blang[react].