Skip to content

Conversation

@IMB11
Copy link
Member

@IMB11 IMB11 commented Nov 23, 2025

  • Creates the new websocket client in the packages/api-client;
    • Uses mitt for event handling
    • Cross platform by design - no custom nuxt impl is needed.
    • Can support multiple server connections (although we probably wont use this for anything user facing)
    • Follows event bus structure
    • Root page connects, child pages/components can subscribe to events safely, as long as they unsubscribe on unmounted.
<script setup lang="ts">
const client = injectModrinthClient();

const unsubscribers = ref<(() => void)[]>([])

onMounted(() => {
    // safeConnect prevents a new connection if one already exists, impl details are irrelevant
    await client.ws.safeConnect(serverId);

    unsubscribers.value.push(
        // Just some examples, obv would be in subcomponents etc.                                                    
        client.ws.on(serverId, 'log', handleLog),
        client.ws.on(serverId, 'stats', handleStats),
        client.ws.on(serverId, 'power-state', handlePowerState),
        client.ws.on(serverId, 'uptime', handleUptime),
        client.ws.on(serverId, 'auth-expiring', handleAuthExpiring),
        client.ws.on(serverId, 'auth-incorrect', handleAuthExpiring),
        client.ws.on(serverId, 'auth-ok', () => {}), // no-op example                    
        client.ws.on(serverId, 'installation-result', handleInstallationResult),
    );
});

onUnmounted(() => {
    unsubscribers.value.forEach(func => func());
	client.ws.disconnect(serverId);
});
</script>

@IMB11 IMB11 requested review from Prospector and tdgao November 23, 2025 09:54
@IMB11 IMB11 added frontend Involves work from the frontend team servers Relates to the Modrinth Servers product labels Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend Involves work from the frontend team servers Relates to the Modrinth Servers product

Development

Successfully merging this pull request may close these issues.

2 participants