diff --git a/content/chat/connect.textile b/content/chat/connect.textile index 1e3a7e461c..c5594ef81b 100644 --- a/content/chat/connect.textile +++ b/content/chat/connect.textile @@ -71,14 +71,11 @@ blang[react]. blang[javascript,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[javascript,swift]. + Use the "@connection.status.onChange()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.ConnectionStatus.html#onChange"@connection.onStatusChange(bufferingPolicy: .unbounded)@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Connection.html#onStatusChange 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/ably-chat-swift/main/typedoc/interfaces/chat.Connection.html#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)); @@ -103,29 +100,29 @@ 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: + +blang[javascript,swift]. + To remove the connection status listener, call the @off()@@unsubscribe()@ function returned in the @subscribe()@ response: ```[javascript] off(); ``` - Use the "@connection.status.offAll()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.ConnectionStatus.html#offAll method to remove all connection status listeners: - - ```[javascript] - chatClient.connection.status.offAll(); + ```[swift] + subscription.unsubscribe() ``` blang[react]. -blang[swift]. - To stop the subscription from firing new status events, call its @unsubscribe()@ method: +blang[javascript]. + Use the "@connection.status.offAll()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.ConnectionStatus.html#offAll method to remove all connection status listeners: - ```[swift] - subscription.unsubscribe() + ```[javascript] + chatClient.connection.status.offAll(); ``` +blang[react,swift]. + 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. @@ -170,16 +167,13 @@ for await error in subscription { blang[react]. -blang[javascript]. - Use the @off()@ function returned in the @onDiscontinuity()@ response to remove a listener: +blang[javascript,swift]. + Use the @off()@@unsubscribe()@ function returned in the @onDiscontinuity()@@subscribeToDiscontinuities()@ response to remove a listener: ```[javascript] off(); ``` -blang[swift]. - To cancel discontinuity subscribtion, call the provided @unsubscribe()@ method: - ```[swift] subscription.unsubscribe() ``` diff --git a/content/chat/rooms/index.textile b/content/chat/rooms/index.textile index 825a9ee671..e512379e6e 100644 --- a/content/chat/rooms/index.textile +++ b/content/chat/rooms/index.textile @@ -240,28 +240,44 @@ const MyComponent = () => { let status = try await room.status ``` -blang[javascript]. +blang[javascript,swift]. You can also subscribe to room status updates by registering a listener. An event will be emitted whenever the status of the room changes. - Use the "@room.status.onChange()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.RoomStatus.html#onChange method in a room to register a listener for status change updates: + Use the "@room.status.onChange()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.RoomStatus.html#onChange"@room.onStatusChange(bufferingPolicy: .unbounded)@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Room.html#onStatusChange method in a room to register a listener for status change updates: ```[javascript] const { off } = room.onStatusChange((change) => console.log(change)); ``` - To remove the room status listener, call the provided @off()@ function: + ```[swift] + let statusSubscription = try await room.onStatusChange(bufferingPolicy: .unbounded) + for await status in statusSubscription { + print("Room status: \(status)") + } + ``` + + To remove the room status listener, call the provided @off()@@unsubscribe()@ function: ```[javascript] off(); ``` + ```[swift] + statusSubscription.unsubscribe() + ``` + +blang[react]. + +blang[javascript]. Use the "@room.status.offAll()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.RoomStatus.html#offAll method to remove all room status listeners in a room: ```[javascript] room.status.offAll(); ``` +blang[swift]. + blang[react]. Listeners can also be registered to monitor the changes in room 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. @@ -280,19 +296,3 @@ blang[react]. return
Occupancy Component
; }; ``` - -blang[swift]. - You can also create a subscribtion to the room status updates. An event will be emitted whenever the status of the room changes. - - ```[swift] - let statusSubscription = try await room.onStatusChange(bufferingPolicy: .unbounded) - for await status in statusSubscription { - print("Room status: \(status)") - } - ``` - - To cancel the room status subscribtion, call the provided @unsubscribe()@ method: - - ```[swift] - statusSubscription.unsubscribe() - ``` diff --git a/content/chat/rooms/messages.textile b/content/chat/rooms/messages.textile index 322c6c558d..1b6f10b448 100644 --- a/content/chat/rooms/messages.textile +++ b/content/chat/rooms/messages.textile @@ -12,17 +12,14 @@ You can send and receive messages in a chat room with any number of participants h2(#subscribe). Subscribe to messages -blang[javascript]. - Subscribe to receive messages in a room by registering a listener. Use the "@messages.subscribe()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Messages.html#subscribe method in a room to receive all messages that are sent to it: +blang[javascript,swift]. + Subscribe to receive messages in a room by registering a listener. Use the "@messages.subscribe()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Messages.html#subscribe"@messages.subscribe(bufferingPolicy: .unbounded)@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Messages.html#subscribe method in a room to receive all messages that are sent to it: blang[react]. Subscribe to messages with the "@useMessages@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/functions/chat-react.useMessages.html hook. Supply a listener and the hook will automatically subscribe to messages sent to the room. As long as a defined value is provided, the subscription will persist across renders. If the listener value is undefined, the subscription will be removed until it becomes defined again. Providing a listener will also enable you to retrieve messages that have been "previously sent to the room.":/chat/rooms/history -blang[swift]. - Subscribe to receive messages in a room by creating a subscription. Use the "@messages.subscribe(bufferingPolicy: .unbounded)@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Messages.html#subscribe method in a room to receive all messages that are sent to it: - ```[javascript] const {unsubscribe} = room.messages.subscribe((message) => { console.log(message); @@ -80,10 +77,10 @@ The following are the properties of a message: h3(#unsubscribe). Unsubscribe from messages -blang[javascript]. +blang[javascript,swift]. Unsubscribe from messages to remove previously registered listeners. - Use the "@unsubscribe()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageSubscriptionResponse.html#unsubscribe function returned in the @subscribe()@ response to remove a listener: + Use the "@unsubscribe()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.MessageSubscriptionResponse.html#unsubscribe"@unsubscribe()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.MessageSubscription.html#unsubscribe function returned in the @subscribe()@ response to remove a listener: ```[javascript] // Initial subscription @@ -93,21 +90,25 @@ blang[javascript]. unsubscribe(); ``` + ```[swift] + messagesSubscription.unsubscribe() + ``` + +blang[react]. + +blang[javascript]. Use the "@messages.unsubscribeAll()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Messages.html#unsubscribeAll method to deregister all chat message listeners in a room: ```[javascript] await room.messages.unsubscribeAll(); ``` +blang[react,swift]. + blang[react]. When you unmount the component that is using the @useMessages@ hook, it will automatically handle unsubscribing any associated listeners registered to receive messages. -blang[swift]. - To cancel the room messages subscribtion, call the provided @unsubscribe()@ method: - - ```[swift] - messagesSubscription.unsubscribe() - ``` +blang[javascript,swift].