Skip to content

Commit

Permalink
Added swift texts and samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Dec 5, 2024
1 parent bbfe712 commit 32ff432
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 61 deletions.
56 changes: 53 additions & 3 deletions content/chat/connect.textile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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/ably-chat-swift/main/typedoc/interfaces/chat.ConnectionStatus.html#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:

Expand All @@ -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.

Expand All @@ -61,14 +69,17 @@ blang[react].
};
```

blang[javascript].
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[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));
```
Expand All @@ -86,6 +97,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:

Expand All @@ -101,14 +119,21 @@ blang[javascript].

blang[react].

blang[swift].
To stop the subscription from firing new status events, call its @unsubscribe()@ method:

```[swift]
subscription.unsubscribe()
```

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.

During periods of discontinuity greater than 2 minutes then you will need to take steps to recover any missed messages, such as by calling "history":/chat/rooms/history.

blang[javascript].
Each feature of the Chat SDK provides an @onDiscontinuity()@ handler to assist with this. These methods enable you to register a listener that will be notified when discontinuity occurs in that feature so that you can handle it, as needed.
blang[javascript,swift].
Each feature of the Chat SDK provides an <span lang="javascript>@onDiscontinuity()@</span><span lang="swift">@subscribeToDiscontinuities()@</span> handler to assist with this. These methods enable you to register a listener that will be notified when discontinuity occurs in that feature so that you can handle it, as needed.

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.
Expand Down Expand Up @@ -136,6 +161,13 @@ const MyComponent = () => {
};
```

```[swift]
let subscription = room.messages.subscribeToDiscontinuities()
for await error in subscription {
print("Recovering from the error: \(error)")
}
```

blang[react].

blang[javascript].
Expand All @@ -145,10 +177,28 @@ blang[javascript].
off();
```

blang[swift].
To cancel discontinuity subscribtion, call the provided @unsubscribe()@ method:

```[swift]
subscription.unsubscribe()
```

blang[javascript,swift].
The following discontinuity handlers are available:

blang[react].

blang[javascript].
* "Messages":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Messages.html#onDiscontinuity
* "Presence":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Presence.html#onDiscontinuity
* "Occupancy":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Occupancy.html#onDiscontinuity
* "Typing indicators":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Typing.html#onDiscontinuity
* "Room reactions":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.RoomReactions.html#onDiscontinuity

blang[swift].
* "Messages":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Messages.html#subscribeToDiscontinuities
* "Presence":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#subscribeToDiscontinuities
* "Occupancy":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Occupancy.html#subscribeToDiscontinuities
* "Typing indicators":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Typing.html#subscribeToDiscontinuities
* "Room reactions":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.RoomReactions.html#subscribeToDiscontinuities
48 changes: 39 additions & 9 deletions content/chat/rooms/history.textile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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.

h2(#get). Retrieve previously sent messages

blang[javascript].
Use the "@messages.get()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Messages.html#get method to retrieve messages that have been previously sent to a room:
blang[javascript,swift].
Use the <span lang="javascript>"@messages.get()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.Messages.html#get</span><span lang="swift">"@messages.get()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Messages.html#get</span> method to retrieve messages that have been previously sent to a room:

blang[react].
Use the "@get()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_react.UseMessagesResponse.html#get method available from the response of the @useMessages@ hook to retrieve messages that have been previously sent to a room.
Expand Down Expand Up @@ -50,20 +51,38 @@ const MyComponent = () => {
};
```

```[swift]
let paginatedResult = try await room.messages.get(options: .init(orderBy: .newestFirst))
print(paginatedResult.items)
if 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

Users can also retrieve historical messages that were sent to a room before the point that they registered a listener by "subscribing":/chat/rooms/messages#subscribe. The order of messages returned is from most recent, to oldest. This is useful for providing conversational context when a user first joins a room, or when they subsequently rejoin it later on. It also ensures that the message history they see is continuous, without any overlap of messages being returned between their subscription and their history call.

blang[javascript].
Use the "@getPreviousMessages()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.MessageSubscriptionResponse.html#getPreviousMessages function returned as part of a "message subscription":/chat/rooms/messages#subscribe response to only retrieve messages that were received before the listener was subscribed to the room:
blang[javascript,swift].
Use the <span lang="javascript>"@getPreviousMessages()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.MessageSubscriptionResponse.html#getPreviousMessages</span><span lang="swift">"@getPreviousMessages()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.MessageSubscription.html#getPreviousMessages</span> function returned as part of a "message subscription":/chat/rooms/messages#subscribe response to only retrieve messages that were received before the listener was subscribed to the room:

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.
Expand Down Expand Up @@ -114,6 +133,17 @@ 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 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 |
Expand Down
Loading

0 comments on commit 32ff432

Please sign in to comment.