I wanted to create a minimalistic chat application using react-router 7 and server sent events. Here we go.
Clone the repo, pnpm install, then pnpm dev.
Open your browser (https://localhost:5173 by default), give yourself a name and then start chatting. After sending a
message with Enter you should see a message pop up in the list below.
Open a second browser and start chatting with yourself.
- the resource route at
/listenreturns atext/event-streamresponse, hence the route serves as an event source - this resource route also automatically creates a
ReadableStream- that includes a heartbeat (see routes/chat-room.server.ts) running every 5 seconds
- sidenote: I increased the
streamTimeoutfrom 5 to 10 seconds (in entry.server.tsx)
- sidenote: I increased the
- registers a callback that can be called everytime a new message is added to the chatroom
- that includes a heartbeat (see routes/chat-room.server.ts) running every 5 seconds
- everytime a message is sent to the server (POSTing to
/) all active event-sourcedReadableStreaminstances are notified