English
Checkpoint
What must work
Run in Source/Chatex.Web:
powershell
npm run buildpowershell
npm test -- --watch=falsepowershell
npm run lintpowershell
npm run format:checkAll of core, state, shared, src/testing and the shell, the list and the composer are final now:
powershell
git diff --no-index --stat Source/Chatex.Web/src/app/core D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\corepowershell
git diff --no-index --stat Source/Chatex.Web/src/app/state D:\Work\Chatex-Reference\Source\Chatex.Web\src\app\statepowershell
git diff --no-index --stat Source/Chatex.Web/src/testing D:\Work\Chatex-Reference\Source\Chatex.Web\src\testingAll three print nothing. Only conversation.page.ts and app.routes.ts still differ, and four feature folders are missing; topic 21 adds them.
Commit:
powershell
git add .powershell
git commit -m "Add the realtime client: events, catch-up after reconnects, presence and typing"Common mistakes
Table 20.2. Common mistakes in the realtime client
| Symptom | Cause | Fix |
|---|---|---|
| a new message appears twice in the sender's window | the store appends events instead of merging them by id with the POST response | merge by id (merge, and the id check in applyMessageCreated) |
| messages sent while the laptop slept are missing after it wakes up | the store relies on events only and does not catch up on connected with reconnected: true | call catchUp() with ?after= after every reconnect |
| after visiting many chats, every event is applied many times and the tab slows down | MessagesStore does not unsubscribe when its page is destroyed | unsubscribe in DestroyRef.onDestroy |
negotiate answers 401 after the client has been open for more than 15 minutes | the connection is built with a fixed token instead of a token factory | use accessTokenFactory: () => auth.getValidToken() |
| "typing…" never disappears after the other user stops | the indicator has no expiry timer | expire it after 5 s, and clear it on messageCreated |
the other user receives dozens of Typing calls per second | every keystroke invokes the hub | throttle to one call every 3 s per chat |
Self-check questions
- Why is there exactly one SignalR connection per tab, and what starts and stops it?
- How does the connection authenticate, and why can it not send an
Authorizationheader? - What is the difference between SignalR's automatic reconnect and the service's own retry after
onclose? - Why are hub events published on a
Subjectand not stored in a signal? - What is a discriminated union? What does TypeScript know inside
case 'messageDeleted':? - Why can a client not rely on events alone? What does it do after a reconnect?
- Which rule decides whether a new message counts as unread?
- Why does
MessagesStoreunsubscribe from the events whileChatsStoredoes not? - How does the presence store learn who is online right after connecting?
- Why is typing throttled on the sender's side and expired on the receivers' side?
Exercises
- Connection state in the rail. Show a small coloured dot on the Chatex logo in the rail: green when connected, amber when reconnecting, grey when disconnected. Read
realtime.state(); no store changes are needed. - A typing store test. Write
typing.store.spec.tswithvi.useFakeTimers(): atypingevent of Bob makesusersTyping('chat-1')return his id, hismessageCreatedclears it at once, and without a message it disappears after 5 seconds. - A new event. Plan a
ChatPinnedevent (a pinned message in a group). List every file of the backend and the client that must change, in order —Docs/Realtime.md("Adding an event") gives the backend half.