Client initializes connection to Ably with generated Token
-
-
-
-
The Ably client has been successfully initialized.
+
+
+ ✗
+
+ Client is connected
+
diff --git a/examples/auth-generate-token/javascript/src/script.ts b/examples/auth-generate-token/javascript/src/script.ts
index c8dcebeb11..c7adc27b32 100644
--- a/examples/auth-generate-token/javascript/src/script.ts
+++ b/examples/auth-generate-token/javascript/src/script.ts
@@ -21,16 +21,19 @@ function handleConnect() {
messageTwo.className = 'text-green-500';
messageTwo.textContent = '✓';
- const headerInitialized = document.getElementById('header-initialized');
- const paragraphInitialized = document.getElementById('paragraph-initialized');
-
- headerInitialized.style.display = 'block';
- paragraphInitialized.style.display = 'block';
-
const button = document.getElementById('connect') as HTMLButtonElement;
button.disabled = true;
button.className = 'px-4 py-2 text-white rounded bg-gray-500';
+
+ realtimeClient.connection.on('connected', (stateChange) => {
+ const messageThree = document.getElementById('message-3');
+ messageThree.className = 'text-green-500';
+ messageThree.textContent = '✓';
+
+ const headingMessage = document.getElementById('heading-message');
+ headingMessage.textContent = 'The Ably client has been successfully initialized.';
+ });
} catch (error) {
- console.error('Failed to initialise client:', error);
+ console.error('Failed to connect client:', error);
}
}
diff --git a/examples/auth-generate-token/react/src/app/page.tsx b/examples/auth-generate-token/react/src/app/page.tsx
index 3299df2ad7..9cbc422f5d 100644
--- a/examples/auth-generate-token/react/src/app/page.tsx
+++ b/examples/auth-generate-token/react/src/app/page.tsx
@@ -9,12 +9,12 @@ interface StatusMessage {
message: string;
}
-// Main Home component
export default function Home() {
const [client, setClient] = useState(null);
const [messages, setMessages] = useState([
{ id: 1, success: false, message: "Client requests token from server" },
- { id: 2, success: false, message: "Client initializes connection to Ably with generated Token" }
+ { id: 2, success: false, message: "Client initializes connection to Ably with generated Token" },
+ { id: 3, success: false, message: "Client is connected"}
]);
const [isLoading, setIsLoading] = useState(false);
@@ -37,6 +37,13 @@ export default function Home() {
)
);
+ ablyClient.connection.on('connected', (stateChange) => {
+ setMessages(prevMessages =>
+ prevMessages.map(msg =>
+ msg.id === 3 ? { ...msg, success: true } : msg
+ )
+ );
+ });
} catch (error) {
console.error('Failed to initialise client:', error);
} finally {
@@ -49,7 +56,11 @@ export default function Home() {
Authentication with Ably
-
Press the Connect button to initialize the client:
+ {client ? (
+
The Ably client has been successfully initialized.
+ ) : (
+
Press the Connect button to initialize the client:
+ )}
-
- {client ? (
-
-
Client Initialized
-
The Ably client has been successfully initialized.
-
- ) : (
-
- )}
);