Skip to content

Commit cd708c1

Browse files
authored
Chat v1 api review (JS) (#2626)
* chat: remove unsubscribeAll methods Removes the `unsubscribeAll` methods from the Chat SDK documentation, as they're being removed. Also fixes an "aside" block that is mistakenly being conditioned to only JS. * chat: remove roomId from message type * chat: event formats * chat: update events enum names * chat: rename getPreviousMessages and messages.get * chat: renaming typing.get to typing.current * chat: add occupancy.current docs * chat: rename roomId to name * chat: rename MessageEvent to ChatMessageEvent * chat: revert mobile changes * chat: remove presence action from member * fixup/chat: fix markdown errors * chat: fix missing roomID change * chat: rename MessageEvents to ChatMessageEventType * chat: update JS edit/delete serials * chat: update message.reactions.add() to send() * chat: update REST docs * chat: update rest spec with room name * chat: bump JS/React to 0.8.0 * chat: typographical updates
1 parent 814bd6f commit cd708c1

File tree

27 files changed

+339
-268
lines changed

27 files changed

+339
-268
lines changed

examples/chat-presence/javascript/src/script.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Ably from 'ably';
2-
import { ChatClient, PresenceEvent, PresenceMember } from '@ably/chat';
2+
import { ChatClient, PresenceEvent, PresenceEventType, PresenceMember } from '@ably/chat';
33
import minifaker from 'minifaker';
44
import 'minifaker/locales/en';
55

@@ -15,73 +15,77 @@ async function initializeChat() {
1515

1616
// Get ROOM with typing capabilities
1717
const room = await chatClient.rooms.get(channelName);
18-
const onlineStatuses = await room.presence.get();
19-
20-
for (const onlineStatus of onlineStatuses) {
21-
await addCard(onlineStatus);
22-
}
2318

2419
/** 💡 Subscribe to the presence set of the room to see online statuses 💡 */
2520
room.presence.subscribe(async (event: PresenceEvent) => {
26-
if (event.action === 'enter') {
27-
await addCard(event);
21+
const presenceMember = event.member;
22+
if (event.type === PresenceEventType.Enter) {
23+
await addCard(presenceMember);
2824

29-
if (event.clientId === realtimeClient.auth.clientId) {
25+
if (presenceMember.clientId === realtimeClient.auth.clientId) {
3026
const button = document.createElement('button');
31-
button.className = 'uk-btn uk-btn-md uk-btn-primary mb-4 rounded-[1998px] w-full min-w-[120px] border uk-border-primary';
27+
button.className =
28+
'uk-btn uk-btn-md uk-btn-primary mb-4 rounded-[1998px] w-full min-w-[120px] border uk-border-primary';
3229
button.id = 'status-button';
3330
button.onclick = async () => {
3431
await room.presence.update({
35-
status: (event.data as { status?: string })?.status === 'Away' ? 'Online' : 'Away',
32+
status: (presenceMember.data as { status?: string })?.status === 'Away' ? 'Online' : 'Away',
3633
});
3734
};
38-
button.textContent = (event.data as { status?: string })?.status === 'Away' ? 'Show online' : 'Set away';
35+
button.textContent =
36+
(presenceMember.data as { status?: string })?.status === 'Away' ? 'Show online' : 'Set away';
3937

4038
const parentDiv = document.getElementById('cards');
4139
if (parentDiv) {
4240
parentDiv.appendChild(button);
4341
}
4442
}
45-
} else if (event.action === 'leave') {
46-
const card = document.getElementById(event.clientId);
43+
} else if (event.type === PresenceEventType.Leave) {
44+
const card = document.getElementById(presenceMember.clientId);
4745
card?.remove();
48-
} else if (event.action === 'update') {
49-
if (event.clientId === realtimeClient.auth.clientId) {
46+
} else if (event.type === PresenceEventType.Update) {
47+
if (presenceMember.clientId === realtimeClient.auth.clientId) {
5048
const button = document.getElementById('status-button');
5149
if (button) {
52-
button.textContent = (event.data as { status?: string })?.status === 'Away' ? 'Show online' : 'Set away';
50+
button.textContent =
51+
(presenceMember.data as { status?: string })?.status === 'Away' ? 'Show online' : 'Set away';
5352
button.onclick = async () => {
5453
await room.presence.update({
55-
status: (event.data as { status?: string })?.status === 'Away' ? 'Online' : 'Away',
54+
status: (presenceMember.data as { status?: string })?.status === 'Away' ? 'Online' : 'Away',
5655
});
5756
};
5857
}
5958
}
60-
const card = document.getElementById(event.clientId);
59+
const card = document.getElementById(presenceMember.clientId);
6160
const statusDiv = card?.querySelector('.text-sm');
6261
if (statusDiv) {
63-
statusDiv.textContent = (event.data as { status?: string })?.status ?? 'Online';
62+
statusDiv.textContent = (presenceMember.data as { status?: string })?.status ?? 'Online';
6463
}
6564

66-
const parentDiv = document.getElementById(event.clientId);
65+
const parentDiv = document.getElementById(presenceMember.clientId);
6766
const onlineStatusDiv = parentDiv?.querySelector('#online-status');
6867
onlineStatusDiv.classList.add(
69-
(event.data as { status?: string })?.status === 'Away' ? 'bg-amber-500' : 'bg-green-500',
68+
(presenceMember.data as { status?: string })?.status === 'Away' ? 'bg-amber-500' : 'bg-green-500',
7069
);
7170
onlineStatusDiv.classList.remove(
72-
(event.data as { status?: string })?.status === 'Away' ? 'bg-green-500' : 'bg-amber-500',
71+
(presenceMember.data as { status?: string })?.status === 'Away' ? 'bg-green-500' : 'bg-amber-500',
7372
);
7473
}
7574
});
7675

7776
/** 💡 Attach the client to a room to begin streaming messages and events to the client.💡 */
7877
await room.attach();
78+
const onlineStatuses = await room.presence.get();
79+
80+
for (const onlineStatus of onlineStatuses) {
81+
await addCard(onlineStatus);
82+
}
7983

8084
/** 💡 Enter presence to appear online 💡 */
8185
await room.presence.enter({ status: 'Online' });
8286
}
8387

84-
async function addCard(onlineStatus: PresenceMember | PresenceEvent) {
88+
async function addCard(onlineStatus: PresenceMember) {
8589
// Create an element to store status items
8690
const card = document.createElement('div');
8791
card.className =

examples/chat-presence/react/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export default function App() {
9898
return (
9999
<div className="min-h-screen">
100100
<ChatClientProvider client={chatClient}>
101-
<ChatRoomProvider id={roomName}>
101+
<ChatRoomProvider name={roomName}>
102102
<Home />
103103
</ChatRoomProvider>
104104
</ChatClientProvider>

examples/chat-room-history/javascript/src/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ document.getElementById('enter-chat').addEventListener('click', async () => {
8181

8282
const getPastMessages = async () => {
8383
/** 💡 Add past 10 messages published to the room 💡 */
84-
const pastMessages = await room.messages.get({ limit: 10 });
84+
const pastMessages = await room.messages.history({ limit: 10 });
8585

8686
pastMessages.items.forEach((message) => {
8787
const messageExists = document.getElementById(message.serial);

examples/chat-room-history/react/src/Chat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ export default function Chat() {
77
const [messages, setMessages] = useState<Message[]>([]);
88
const [message, setMessage] = useState('');
99
const { clientId } = useChatClient();
10-
const { get, send } = useMessages({
10+
const { history, send } = useMessages({
1111
listener: (event) => {
1212
setMessages((prevMessages: Message[]) => [...prevMessages, event.message]);
1313
},
1414
});
1515

1616
const getPastMessages = () => {
17-
get({ limit: 10 }).then((result) => {
17+
history({ limit: 10 }).then((result) => {
1818
result.items.forEach((message) => {
1919
const messageExists = messages.some((prevMessage) => prevMessage.serial === message.serial);
2020

examples/chat-room-messages/react/src/App.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
22
import {
33
Message,
44
ChatClient,
5+
ChatMessageEvent,
56
} from '@ably/chat';
67
import {
78
useRoom,
@@ -24,8 +25,8 @@ const Chat = () => {
2425
const [message, setMessage] = useState('');
2526
const { clientId } = useChatClient();
2627
const { send } = useMessages({
27-
listener: (message: Message) => {
28-
setMessages((prevMessages: Message[]) => [...prevMessages, message.message]);
28+
listener: (event: ChatMessageEvent) => {
29+
setMessages((prevMessages: Message[]) => [...prevMessages, event.message]);
2930
},
3031
});
3132

@@ -110,7 +111,7 @@ export default function App() {
110111
return (
111112
<div className="min-h-screen">
112113
<ChatClientProvider client={chatClient}>
113-
<ChatRoomProvider id={roomName}>
114+
<ChatRoomProvider name={roomName}>
114115
<ChatRoomMessagesDemo />
115116
</ChatRoomProvider>
116117
</ChatClientProvider>

examples/chat-room-reactions/javascript/src/script.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Ably from 'ably';
2-
import { ChatClient, Room } from '@ably/chat';
2+
import { ChatClient, Room, RoomReactionEvent } from '@ably/chat';
33
import { nanoid } from 'nanoid';
44
import './styles.css';
55

@@ -17,7 +17,8 @@ async function initializeChat() {
1717
room = await chatClient.rooms.get(roomName);
1818

1919
/** 💡 Add every room reaction published to the room 💡 */
20-
room.reactions.subscribe((reaction) => {
20+
room.reactions.subscribe((reactionEvent: RoomReactionEvent) => {
21+
const reaction = reactionEvent.reaction;
2122
const reactionsContainer = document.getElementById('reaction-area');
2223

2324
const reactionElement = document.createElement('span');

examples/chat-room-reactions/react/src/App.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { ChatClient, Reaction as ReactionInterface } from '@ably/chat';
2+
import { ChatClient, Reaction as ReactionInterface, RoomReactionEvent } from '@ably/chat';
33
import { ChatClientProvider, ChatRoomProvider, useRoom, useRoomReactions } from '@ably/chat/react';
44
import { Realtime } from 'ably';
55
import './styles/styles.css';
@@ -15,7 +15,8 @@ function Chat() {
1515
const emojis = ['❤️', '😲', '👍', '😊'];
1616

1717
const { send } = useRoomReactions({
18-
listener: (reaction) => {
18+
listener: (reactionEvent: RoomReactionEvent) => {
19+
const reaction = reactionEvent.reaction;
1920
setReactions((prevReactions: ReactionInterface[]) => [...prevReactions, { ...reaction }]);
2021

2122
setTimeout(() => {
@@ -106,7 +107,7 @@ export default function App() {
106107

107108
return (
108109
<ChatClientProvider client={chatClient}>
109-
<ChatRoomProvider id={roomName}>
110+
<ChatRoomProvider name={roomName}>
110111
<ChatRoomReactionsDemo />
111112
</ChatRoomProvider>
112113
</ChatClientProvider>

examples/chat-typing-indicator/javascript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Use the following methods to add typing indicators into a chat application:
1414

1515
- [`rooms.get()`](/docs/chat/rooms?lang=javascript#create): creates a new or retrieves an existing `room`.
1616
- [`rooms.typing.subscribe()`](/docs/chat/rooms/typing#subscribe): subscribes to typing events by registering a listener. Typing events are emitted when a user starts typing, or when they stop typing.
17-
- [`room.typing.get()`](/docs/chat/rooms/typing?lang=javascript#get): retrieves the list of users currently typing by their clientId.
17+
- [`room.typing.current()`](/docs/chat/rooms/typing?lang=javascript#get): retrieves the list of users currently typing by their clientId.
1818
- [`room.typing.keystroke()`](/docs/chat/rooms/typing?lang=javascript#keystroke): emits a typing event that the user is currently typing, initialising the timeout which will call `room.typing.stop()` if no further events are emitted by the user.
1919

2020
Find out more about [typing indicators](/docs/chat/rooms/typing).

examples/chat-typing-indicator/react/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export default function App() {
8888
<div className="min-h-screen">
8989
<ChatClientProvider client={chatClient}>
9090
<ChatRoomProvider
91-
id={roomName}
91+
name={roomName}
9292
options={{
9393
typing: typingOptions,
9494
}}

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"spaces-member-location-react": "yarn workspace spaces-member-location-react dev"
9595
},
9696
"dependencies": {
97-
"@ably/chat": "^0.7.0",
97+
"@ably/chat": "^0.8.0",
9898
"@ably/spaces": "^0.4.0",
9999
"ably": "^2.9.0",
100100
"cors": "^2.8.5",

examples/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# yarn lockfile v1
33

44

5-
"@ably/chat@^0.7.0":
6-
version "0.7.0"
7-
resolved "https://registry.yarnpkg.com/@ably/chat/-/chat-0.7.0.tgz#4e597a50c92ba14c2f79162e745ee224e0fb5ae0"
8-
integrity sha512-m7gChuLoXcB2KWDAwyLBfh+V5YJf3v5aweml+zdIwt5/JNztChmolN4pqJhEv0B5dT21tQU/fWkFtcyXBFJOvQ==
5+
"@ably/chat@^0.8.0":
6+
version "0.8.0"
7+
resolved "https://registry.yarnpkg.com/@ably/chat/-/chat-0.8.0.tgz#3a14729a537978014466810c00500cbac7e2206a"
8+
integrity sha512-t85MfK+WepO3282QxolZ/BCwmO3QtIoOjOgmEYyDhUvW/SdexQsj//ZSWDrP0ZXsELV2q5fm14FO5cATwHd35Q==
99
dependencies:
1010
async-mutex "^0.5.0"
1111
dequal "^2.0.3"

src/components/Examples/ExamplesRenderer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const getDependencies = (id: string, products: string[], activeLanguage: Languag
4141
nanoid: '^5.0.7',
4242
minifaker: '1.34.1',
4343
...(products.includes('auth') ? { cors: '^2.8.5' } : {}),
44-
...(products.includes('chat') ? { '@ably/chat': '^0.7.0' } : {}),
44+
...(products.includes('chat') ? { '@ably/chat': '^0.8.0' } : {}),
4545
...(products.includes('spaces') ? { '@ably/spaces': '^0.4.0' } : {}),
4646
...(id === 'spaces-component-locking' ? { 'usehooks-ts': '^3.1.0' } : {}),
4747
...(activeLanguage === 'react' || products.includes('chat') || products.includes('spaces')

src/data/languages/languageData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export default {
2222
go: 1.2,
2323
},
2424
chat: {
25-
javascript: 0.7,
26-
react: 0.7,
25+
javascript: 0.8,
26+
react: 0.8,
2727
swift: 0.4,
2828
kotlin: 0.3,
2929
},

src/pages/docs/chat/connect.mdx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@ subscription.unsubscribe()
134134
</Code>
135135
</If>
136136

137-
<If lang="javascript">
138-
Use the [`connection.offAllStatusChange()`](https://sdk.ably.com/builds/ably/ably-chat-js/main/typedoc/interfaces/chat-js.Connection.html#offAllStatusChange) method to remove all connection status listeners:
139-
140-
<Code>
141-
```javascript
142-
chatClient.connection.offAllStatusChange();
143-
```
144-
</Code>
145-
</If>
146-
147137
## Handle connection discontinuity <a id="discontinuity"/>
148138

149139
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.

src/pages/docs/chat/getting-started/javascript.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Create an `index.ts` file in your project and add the following function to inst
8080
<Code>
8181
```javascript
8282
import * as Ably from 'ably';
83-
import { ChatClient, MessageEvent, Message } from '@ably/chat';
83+
import { ChatClient, ChatMessageEvent, Message } from '@ably/chat';
8484

8585
async function getStarted() {
8686

@@ -110,7 +110,7 @@ const room = await chatClient.rooms.get('my-first-room');
110110

111111
await room.attach();
112112

113-
room.messages.subscribe((messageEvent: MessageEvent) => {
113+
room.messages.subscribe((messageEvent: ChatMessageEvent) => {
114114
console.log(`Received message: ${ messageEvent.message.text }`);
115115
});
116116

@@ -167,12 +167,12 @@ async function getStartedLate() {
167167

168168
await room.attach();
169169

170-
const { getPreviousMessages } = room.messages.subscribe(() => {
170+
const { historyBeforeSubscribe } = room.messages.subscribe(() => {
171171
console.log('New message received');
172172
});
173173

174174

175-
const historicalMessages = await getPreviousMessages({ limit: 10 });
175+
const historicalMessages = await historyBeforeSubscribe({ limit: 10 });
176176
console.log(historicalMessages.items.map((message) => `${message.clientId}: ${message.text}`));
177177

178178
};

0 commit comments

Comments
 (0)