Skip to main content

Real-time Chat Application

Learn how to build a full-featured real-time chat application using kSync with WebSocket synchronization, React hooks, and event-sourcing.

Overview

This example demonstrates:
  • Real-time messaging with WebSocket sync
  • User presence and typing indicators
  • Message history with pagination
  • Optimistic updates for instant UX
  • Offline support with automatic reconnection

Server Setup

First, let’s create a simple WebSocket server:
server.ts

Chat Store Setup

Create the chat store with kSync:
stores/chatStore.ts

React Chat Component

Create the main chat component using kSync React hooks:
components/ChatRoom.tsx

Message Components

components/MessageList.tsx

Message Input Component

components/MessageInput.tsx

Chat Header Component

components/ChatHeader.tsx

App Component

App.tsx

Styling

styles/chat.css

Features Demonstrated

This chat application showcases many kSync features:
  1. Real-time Sync: Messages appear instantly across all connected clients
  2. Event Sourcing: All chat events are stored and can be replayed
  3. Optimistic Updates: Messages appear immediately before server confirmation
  4. Presence: Shows who’s online and typing
  5. Automatic Reconnection: Handles network disruptions gracefully
  6. React Integration: Uses kSync React hooks for clean component integration

Running the Example

  1. Install dependencies:
  1. Start the server:
  1. Start the React app:
  1. Open multiple browser tabs to see real-time synchronization in action!

Next Steps

Extend this example with:
  • Message persistence with SQLite storage
  • File uploads and media messages
  • Message reactions and threading
  • Push notifications
  • End-to-end encryption
  • Mobile app with React Native