# An Introduction to the StateManager
The `client.stateManager`(`stateManager` for short), is the simple way of communicating and syncing with the state on the `CoreServer`. If you already understand what 'state-based' means, then you may skip this page.

The `stateManager` is the way you would fetch tournaments, get matches, or do anything related to getting specific data that does not require a database query. It is best if you are familiar with what methods it has and how you can use them. 

For the ease of this explanation let me provide an example:
```ts
onMount(async() => {
    // Check if the user is logged in
    if ($authTokenStore) {
        // Fetch the match data that we need to display (current map etc. This function will appear later in docs too)
        await fetchMatchData();
        // Using stateManger listen to global changes to our match
        client.stateManager.on('matchDeleted', handleMatchDeleted);
        client.stateManager.on('matchUpdated', handleMatchUpdated);
        client.stateManager.on('userDisconnected', handleUserDisconnected);
        client.stateManager.on('userUpdated', handleUserUpdated);
        // Using the client listen to events that will not be stored in the state, such as scores and song finish events
        client.on('realtimeScore', handleRealtimeScoreUpdate);
        client.on('songFinished', handleSongFinished);

        // This is a helper function you can ignore, it just prevents the user from reloading the page while in the match
        window.addEventListener('beforeunload', beforeUnloadHandler);
    } else {
        // If the user is not authenticated, they will be thrown to the discord authentication page
        window.location.href = "/discordAuth"
    }
});

onDestroy(() => {
    // Remove the listeners that were added on mount
    client.stateManager.removeListener('matchUpdated', handleMatchUpdated);
    client.stateManager.removeListener('matchDeleted', handleMatchDeleted);
    client.stateManager.removeListener('userDisconnected', handleUserDisconnected);
    client.stateManager.removeListener('userUpdated', handleUserUpdated);
    client.removeListener('realtimeScore', handleRealtimeScoreUpdate);
    client.removeListener('songFinished', handleSongFinished);

    // Remove the no-refresh behaviour
    window.removeEventListener('beforeunload', beforeUnloadHandler);
});
```
<div class="info-box">
    <span class="material-icons">info</span>
    <div>
        <strong>Example:</strong> This is an example from the page of ShyyTAUI within the match coorination page. 
    </div>
</div>