From 229a6ff6db506d436f9b8bb70d5904f6c520629f Mon Sep 17 00:00:00 2001 From: Marat Al Date: Wed, 27 Nov 2024 18:31:38 +0100 Subject: [PATCH] Added swift texts and samples for connection, rooms, messages, presence, occupancy, reactions and typings. --- content/chat/connect.textile | 37 +++++++++++++++ content/chat/rooms/index.textile | 68 ++++++++++++++++++++++++---- content/chat/rooms/messages.textile | 25 ++++++++++ content/chat/rooms/occupancy.textile | 22 +++++++++ content/chat/rooms/presence.textile | 55 +++++++++++++++++++++- content/chat/rooms/reactions.textile | 29 +++++++++++- content/chat/rooms/typing.textile | 35 +++++++++++++- 7 files changed, 259 insertions(+), 12 deletions(-) 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/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].