Users must be identified to enter into the presence set. This means that they must set a @clientId@ when "instantiating their client":/chat/setup#instantiate.
-blang[javascript]. - Use the "@presence.enter()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Presence.html#enter method to indicate when a user joins a room. This will send a presence event to all users subscribed to presence indicating that a new member has joined the chat. You can also set an optional data field with information such as the status of a user: +blang[javascript,swift]. + Use the "@presence.enter()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#enter method to indicate when a user joins a room. This will send a presence event to all users subscribed to presence indicating that a new member has joined the chat. You can also set an optional data field with information such as the status of a user: blang[react]. Indicate when a user joins a room with the "@usePresence@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/functions/chat_react.usePresence.html hook. Users will automatically be entered into the presence set when the component mounts. @@ -159,8 +185,12 @@ const MyComponent = () => { }; ``` -blang[javascript]. - Use the "@presence.update()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Presence.html#update method when a user wants to update their data, such as an update to their status, or to indicate that they're raising their hand. Updates will send a presence event ao all users subscribed to presence: +```[swift] +try await room.presence.enter(data: .init(userCustomData: ["status": .string("Online")])) +``` + +blang[javascript,swift]. + Use the "@presence.update()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#update method when a user wants to update their data, such as an update to their status, or to indicate that they're raising their hand. Updates will send a presence event to all users subscribed to presence: blang[react]. Use the "@update()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_react.UsePresenceResponse.html#update property available from the response of the @usePresence@ hook to update a user's data, such as setting their status to 'Away from keyboard'. @@ -191,8 +221,12 @@ const MyComponent = () => { }; ``` +```[swift] +try await room.presence.update(data: .init(userCustomData: ["status": .string("Busy")])) +``` + blang[javascript]. - Use the "@presence.leave()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Presence.html#leave method to explicitly remove a user from the presence set. This will send a presence event to all users subscribed to presence. You can also set an optional data field such as setting a status of 'Back later'. + Use the "@presence.leave()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#leave method to explicitly remove a user from the presence set. This will send a presence event to all users subscribed to presence. You can also set an optional data field such as setting a status of 'Back later'. blang[react]. Indicate when a user leaves a room with the "@usePresence@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/functions/chat_react.usePresence.html hook. Users will automatically be removed from the presence set when the component unmounts. @@ -218,6 +252,10 @@ const MyComponent = () => { }; ``` +```[swift] +try await room.presence.leave(data: .init(userCustomData: ["status": .string("Be back later!")])) +``` + When a user goes offline or closes their "connection":/chat/connect, a leave event is also emitted and they are removed from the presence set. h2(#options). Presence options @@ -237,10 +275,10 @@ blang[react]. h2(#retrieve). Retrieve the presence set -blang[javascript]. +blang[javascript,swift]. The online presence of users can be retrieved in one-off calls. This can be used to check the status of an individual user, or return the entire presence set as an array. - Use the "@presence.get()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Presence.html#get method to retrieve an array of all users currently entered into the presence set, or individual users: + Use the "@presence.get()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#get method to retrieve an array of all users currently entered into the presence set, or individual users: ```[javascript] // Retrieve all users entered into presence as an array: @@ -250,15 +288,32 @@ blang[javascript]. const presentMember = await room.presence.get({ clientId: 'clemons123' }); ``` - Alternatively, use the "@presence.isUserPresent()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Presence.html#isUserPresent method and pass in a user's @clientId@ to check whether they are online or not. This will return a boolean: + ```[swift] + // Retrieve all users entered into presence as an array: + let presentMembers = try await room.presence.get() + + // Retrieve the status of specific users by their clientId: + let presentMember = try await room.presence.get(params: .init(clientID: "clemons123")) + ``` + +blang[react]. + +blang[javascript,swift]. + Alternatively, use the "@presence.isUserPresent()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#isUserPresent method and pass in a user's @clientId@ to check whether they are online or not. This will return a boolean: ```[javascript] const isPresent = await room.presence.isUserPresent('clemons123'); ``` + ```[swift] + let isPresent = try await room.presence.isUserPresent(clientID: "clemons123") + ``` + blang[react]. Use the "@presenceData@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_react.UsePresenceListener.html#presenceData property available from the response of the @usePresence@ hook to view a list of all member's presence status in the room. +blang[javascript,swift]. + h3(#member-structure). Presence member structure The following is the structure of an individual presence member within the presence set: diff --git a/content/chat/rooms/reactions.textile b/content/chat/rooms/reactions.textile index e4c3dad798..3606cb2031 100644 --- a/content/chat/rooms/reactions.textile +++ b/content/chat/rooms/reactions.textile @@ -5,6 +5,7 @@ product: chat languages: - javascript - react + - swift --- Users can send reactions to the entire chat room to show their sentiment as to what is happening. For example, agreeing with the content in a livestream using a thumbs up, or sending a heart when their team scores in a sports game. @@ -17,11 +18,16 @@ Room reactions are ephemeral and not stored or aggregated by Ably. The intention h2(#subscribe). Subscribe to room reactions +Room reactions events are emitted whenever a user sends a reaction, such as by hitting a 'like' button or clicking a heart emoji. + blang[javascript]. - Subscribe to room reactions by registering a listener. These events are emitted whenever a user sends a reaction, such as by hitting a 'like' button or clicking a heart emoji. Use the "@reactions.subscribe()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.RoomReactions.html#subscribe method in a room to receive reactions: + Subscribe to room reactions by registering a listener. Use the "@reactions.subscribe()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.RoomReactions.html#subscribe method in a room to receive reactions: blang[react]. Subscribe to room reactions with the "@useRoomReactions@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/functions/chat_react.useRoomReactions.html hook. Supply an optional listener to receive the room reactions. + +blang[swift]. + Subscribe to a room reactions by creating a subscription. Use the "@reactions.subscribe(bufferingPolicy: .unbounded)@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.RoomReactions.html#subscribe method in a room to receive reactions: ```[javascript] const {unsubscribe} = room.reactions.subscribe((reaction) => { @@ -42,6 +48,13 @@ const MyComponent = () => { }; ``` +```[swift] +let reactionSubscription = try await room.reactions.subscribe(bufferingPolicy: .unbounded) +for await reaction in reactionSubscription { + print("Received a reaction of type \(reaction.type), and metadata \(reaction.metadata)") +} +``` + h3(#structure). Room reaction event structure The following is the structure of a room reaction event: @@ -95,10 +108,17 @@ blang[javascript]. blang[react]. When you unmount the component that is using the @useRoomReactions@ hook, it will automatically handle unsubscribing any associated listeners registered for room reactions. +blang[swift]. + To cancel the room reactions subscribtion, call the provided @unsubscribe()@ method: + + ```[swift] + reactionSubscription.unsubscribe() + ``` + h2(#send). Send a room reaction -blang[javascript]. - Use the "@reactions.send()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.RoomReactions.html#send method to send a room-level reaction. The most common way of using this method is to trigger it whenever a user clicks an emoji button in a room: +blang[javascript,swift]. + Use the "@reactions.send()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.RoomReactions.html#send method to send a room-level reaction. The most common way of using this method is to trigger it whenever a user clicks an emoji button in a room: blang[react]. Use the "@send()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_react.UseRoomReactionsResponse.html#send method available from the response of the @useRoomReactions@ hook to emit an event when a user reacts, for example when they click an emoji button: @@ -126,3 +146,10 @@ const MyComponent = () => { ); }; ``` + +```[swift] +try await room.reactions.send(params: .init(type: "like")) + +try await room.reactions.send(params: .init(type: "heart", + metadata: ["effect": .string("fireworks")])) +``` diff --git a/content/chat/rooms/typing.textile b/content/chat/rooms/typing.textile index cb3f20077a..97f93bbcc9 100644 --- a/content/chat/rooms/typing.textile +++ b/content/chat/rooms/typing.textile @@ -5,9 +5,10 @@ product: chat languages: - javascript - react + - swift --- -Typing indicators enable you to display which users are currently writing a message in a room. This feature can be used to display a message such as __Sandi is typing...__, or when a certain threshold is reached you could instead display __Multiple people are typing...__ or _12 people are typing..._. +Typing indicators enable you to display which users are currently writing a message in a room. This feature can be used to display a message such as __Sandi is typing...__, or when a certain threshold is reached you could instead display __Multiple people are typing...__ or _12 people are typing..._. Typing events can be emitted when a user starts or stops typing.