Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 2x 2x 22x 22x 22x 22x 22x 6x 4x 2x 2x 24x 23x 24x 7x 16x 16x 16x 15x 15x 15x 15x 15x 2x 2x 2x 2x 2x 15x 7x 7x 7x 1x 1x 5x 1x 4x 1x 3x 1x 1x 15x 6x 6x 15x 1x 3x 3x 1x 1x 3x 3x 22x | // @implements UC-DM-006.1, UC-NOTIF-002.1
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useMessagesStore } from '@/stores/messages'
import { useNotificationsStore, type AppNotification } from '@/stores/notifications'
import { useFlowsStore } from '@/stores/flows'
export type SocketConnectionState = 'connecting' | 'open' | 'closed'
const INITIAL_BACKOFF_MS = 1000
const MAX_BACKOFF_MS = 30000
/**
* Thin wrapper around a single indefinite WebSocket connection to the
* realtime DM push endpoint (see flows-api's WebSocketStack), mounted once
* app-wide from FlowsLayout.vue. Reconnects with exponential backoff on an
* unexpected close, and re-syncs the authoritative unread count via REST
* (messagesStore.fetchUnreadCount) on every successful (re)connect and tells the
* store so an open conversation re-syncs its messages too — that covers
* messages that arrived while disconnected, since a WebSocket push that never
* made it to any device is otherwise silently lost.
*
* The same connection carries notifications (`type: 'notification'`), handled
* the same way through stores/notifications.ts.
*
* No app-level message ever needs to be *sent* over this socket — sending a
* chat message stays a REST POST (see stores/messages.ts) — so this only
* ever reads incoming payloads.
*/
export function useMessagesSocket() {
const connectionState = ref<SocketConnectionState>('closed')
let socket: WebSocket | null = null
let backoffMs = INITIAL_BACKOFF_MS
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
let stopped = false
function scheduleReconnect() {
if (stopped) return
reconnectTimer = setTimeout(() => {
backoffMs = Math.min(backoffMs * 2, MAX_BACKOFF_MS)
void connect()
}, backoffMs)
}
async function connect() {
if (stopped) return
const wsBaseUrl = import.meta.env.VITE_WS_URL
if (!wsBaseUrl) {
// Realtime push isn't configured for this environment — the app still
// works fully via REST, just without live updates.
return
}
const authStore = useAuthStore()
const token = await authStore.getAccessToken()
if (!token) return
const messagesStore = useMessagesStore()
const notificationsStore = useNotificationsStore()
connectionState.value = 'connecting'
socket = new WebSocket(`${wsBaseUrl}?token=${encodeURIComponent(token)}`)
socket.onopen = () => {
connectionState.value = 'open'
backoffMs = INITIAL_BACKOFF_MS
void messagesStore.fetchUnreadCount()
void notificationsStore.fetchUnreadCount()
messagesStore.notifyRealtimeConnected()
}
socket.onmessage = (event) => {
try {
const payload = JSON.parse(event.data)
if (payload?.type === 'message' && payload.conversationId && payload.message) {
messagesStore.receivePushedMessage(payload.conversationId, payload.message)
void messagesStore.fetchUnreadCount()
} else if (payload?.type === 'conversationUpdated' && payload.conversationId) {
void messagesStore.receiveConversationUpdate(payload.conversationId)
} else if (payload?.type === 'notification' && payload.notification) {
void notificationsStore.receivePushedNotification(payload.notification as AppNotification)
} else if (payload?.type === 'permissionsChanged') {
// The user's roles changed in a flow: reload their permissions
void useFlowsStore().refreshMyFlows()
}
} catch (error) {
console.error('[useMessagesSocket] Failed to parse pushed payload:', error)
}
}
socket.onclose = () => {
connectionState.value = 'closed'
scheduleReconnect()
}
socket.onerror = () => {
socket?.close()
}
}
function stop() {
stopped = true
if (reconnectTimer !== null) {
clearTimeout(reconnectTimer)
reconnectTimer = null
}
socket?.close()
socket = null
}
return { connectionState, connect, stop }
}
|