Learn to track users, manage message history, and handle timeouts in a real-time chat application
"view-join"
and "view-exit"
events to track connectionsviewId
as unique identifierfuture()
and model.now()
ChatModel
that handles four main responsibilities:
Map
instead of plain objects for key-value collections to ensure identical behavior across users. Maps maintain insertion order when serialized/deserialized.viewId
already exists (for reconnections)viewId
remains the same if a user reconnects from the same device. However, each browser tab gets a different viewId
, even on the same device.viewId
from the event datainactivity_timeout_ms
milliseconds have passed since the last post. If another post arrived during the timeout period, lastPostTime
will be different, and the reset is skipped.
this.now()
) rather than wall-clock time. This ensures consistent behavior across all users regardless of their local time settings./reset
Math.random()
calls from within the model are deterministic and synchronized.Store Model Reference
Initialize from Model State
Subscribe to Updates
Direct Reading Only
/reset
command. Note that this.viewId
is automatically available in all View classes.
modelOnly()
Model.modelOnly()
throws an error if called outside normal model execution. Use this in setter methods to prevent accidental view access."input"
, "history"
, "viewInfo"
) to organize eventsviewId
as unique identifier for user-specific datafuture()
methodview-join
viewId
. Sent to all users including the one who joined.view-exit
viewId
. Not sent to the user who left.Event Scope
this.sessionId
as the scope, which is the default for system-generated events.