English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet format Chatex.slnx --verify-no-changespowershell
dotnet test --solution Chatex.slnxCommit:
powershell
git add .powershell
git commit -m "Push chat events to the browsers with a SignalR hub, presence and typing"Module 4 is complete: the backend of Chatex is finished. Module 5 builds the Angular client on top of it.
Common mistakes
Table 14.1. Common mistakes in the SignalR layer
| Symptom | Cause | Fix |
|---|---|---|
| no events arrive although messages are saved | AddCore runs before AddApi, so the NullChatNotifier was registered first | call AddApi before AddCore |
the connection is refused with 401 although the token is valid | the token in the query string is not read | the OnMessageReceived event for paths under /hubs |
Context.UserIdentifier is null | SignalR looks for NameIdentifier, the token has sub | register SubClaimUserIdProvider |
| an added member sees new messages only after a reload | the member's connections are not added to the group | add them in MembersAddedAsync before sending |
| users flicker offline on every page reload | "offline" is sent on the last disconnect without delay | the grace period of PresenceService |
| the hub tests hang until the timeout | the test acts before the connection joined its groups | invoke a hub method after StartAsync, as ConnectHubAsync does |
| enums arrive as numbers in events | the JSON protocol of SignalR has no enum converter | AddJsonProtocol with JsonStringEnumConverter |
Self-check questions
- What does SignalR add to plain WebSockets? Which transports can it use?
- Why does each chat have a group, and what would go wrong if events were sent to
Clients.All? - What does the strongly typed hub
Hub<IChatClient>give compared withClients.All.SendAsync("MessageCreated", ...)? - Why does
PresenceTrackercount connections instead of a flag per user? - Why does the hub register the connection before reading the user's chats?
- Why does "offline" wait for a grace period? How does the ticket ensure that only the latest disconnect broadcasts?
- Why does Alice receive her own message as an event too, and why is that not a problem?
- Why are tokens accepted in the query string only on
/hubspaths? - Why must
AddApibe called beforeAddCore? - How can a test prove that an event was not sent?
- What would have to change to run two API servers behind Nginx?
Exercises
- Read receipts in the hub. Instead of
POST /api/chats/{id}/read, a client could call a hub methodMarkRead(chatId, messageId). Write it in the hub usingIChatService. What are the advantages and disadvantages compared with the HTTP endpoint? - A typing test for channels. Should a subscriber's typing be shown in a channel? Change
Typingso that only members who may post can send it, and write the test. - Scale out on paper. Sketch how
PresenceTrackerwould work with two servers and a Redis backplane: what would be stored in Redis, and which method would become a network call?