Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-5135] Updates the Chat docs to include code examples for chat-swift #2315

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
64 changes: 54 additions & 10 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].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's only the link that will be different then I think it may be clearer to combine it into a single block, e.g.:

blang[javascript,swift].
  Use the <span lang="javascript>"@current@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat_js.ConnectionStatus.html#current</span><span lang="swift"></span> property to check which status a connection is currently in:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, here the property name is also different (which is probably something that should be fixed fyi @AndyTWF @lawrence-forooghian @ttypic)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke to Andy about this separately and it has been updated for JS in a separate PR (#2300).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That PR modifies the same lines, I guess it should be merged first before me to proceed with this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

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,14 @@ 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[javascript,swift].
Use the <span lang="javascript">"@connection.status.onChange()@":https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.ConnectionStatus.html#onChange</span><span lang="swift">"@connection.onStatusChange()@":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Connection.html#onStatusChange</span> 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.

```[javascript]
const { off } = chatClient.connection.status.onStatusChange((change) => console.log(change));
```
Expand All @@ -86,28 +94,42 @@ const MyComponent = () => {
};
```

blang[javascript].
To remove the connection status listener, call the @off()@ function returned in the @subscribe()@ response:
```[swift]
let subscription = chatClient.connection.onStatusChange()
for await statusChange in subscription {
print("Connection status changed to: \(statusChange.current)")
}
```

blang[javascript,swift].
To remove the connection status listener, call the <span lang="javascript">@off()@</span><span lang="swift">@unsubscribe()@</span> function returned in the @subscribe()@ response:

```[javascript]
off();
```

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

blang[react].

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:

```[javascript]
chatClient.connection.status.offAll();
```

blang[react].
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.

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].
blang[javascript,swift].
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[react].
Expand Down Expand Up @@ -136,19 +158,41 @@ const MyComponent = () => {
};
```

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

blang[react].

blang[javascript].
Use the @off()@ function returned in the @onDiscontinuity()@ response to remove a listener:
blang[javascript,swift].
Use the <span lang="javascript">@off()@</span><span lang="swift">@unsubscribe()@</span> function returned in the @onDiscontinuity()@ response to remove a listener:

```[javascript]
off();
```

```[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#onDiscontinuity
* "Presence":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Presence.html#onDiscontinuity
* "Occupancy":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Occupancy.html#onDiscontinuity
* "Typing indicators":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.Typing.html#onDiscontinuity
* "Room reactions":https://sdk.ably.com/builds/ably/ably-chat-swift/main/typedoc/interfaces/chat.RoomReactions.html#onDiscontinuity
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. |
Comment on lines +73 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not have parity between JS and Swift? @AndyTWF

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This divergence hasn't yet had a DR and thus hasn't been incorporated into JS - it was sorta an informal Slack conversation.

I suggest we allow the lack of parity for now until we get a chance to update JS - shouldn't take long but I don't want to block this PR getting merged.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is now agreed on orderBy with newestFirst and oldestFirst @m-hulbert


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()
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