Monitor and Manage Connection State
mainYou can monitor the connection status via z.connectionState. Possible states include: connecting, connected, disconnected, needs-auth, error, and closed.
If the connection enters an error or auth-required state, you can manually retry using z.connection.connect(), optionally passing a new auth token.
<script lang="ts">
import { z } from '$lib/zero.svelte';
</script>
{#if z.connectionState.name === 'connected'}
<span>Connected</span>
{:else if z.connectionState.name === 'connecting'}
<span>Connecting...</span>
{:else if z.connectionState.name === 'disconnected'}
<span>Disconnected: {z.connectionState.reason}</span>
{:else if z.connectionState.name === 'needs-auth'}
<span>
Auth Required
<button onclick={() => z.connection.connect()}>Retry</button>
</span>
{:else if z.connectionState.name === 'error'}
<span>
Error: {z.connectionState.reason}
<button onclick={() => z.connection.connect()}>Retry</button>
</span>
{:else if z.connectionState.name === 'closed'}
<span>Closed</span>
{/if}// Retry connection
await z.connection.connect();
// Retry with new auth token
await z.connection.connect({ auth: newToken });