A demonstration of cross-site communication between React and Angular applications using window.postMessage
API.
This project showcases seamless data exchange between two different web applications running on separate ports:
- React App (Main Interface) - Todo list management and display
- Angular App (Popup Form) - Todo creation and editing form
βββββββββββββββββββ window.postMessage ββββββββββββββββββββ
β React App β βββββββββββββββββββββββΊ β Angular App β
β (Port 5173) β Cross-Origin Comm β (Port 4200) β
β β β β
β β’ Todo Display β β β’ Form Creation β
β β’ List Managementβ β β’ Data Validationβ
β β’ Click-to-Edit β β β’ Submit Handler β
βββββββββββββββββββ ββββββββββββββββββββ
- Create Todos - Click "Create New Todo" to open Angular form
- Edit Todos - Click any existing todo to edit it
- Delete Todos - Individual delete buttons for each todo
- Priority System - Color-coded priority levels (High/Medium/Low)
- Timestamps - Creation and update tracking
- Cross-Origin Communication - Secure message passing between apps
- Window Management - Automatic popup handling and closure detection
- Data Validation - Form validation in Angular with error handling
- State Management - Persistent todo storage in React state
- Responsive Design - Clean, modern UI that works across devices
Cross Site/
βββ react-app/ # Main React application
β βββ src/
β β βββ App.jsx # Main component with todo management
β β βββ App.css # Styling for React app
β β βββ main.jsx # React entry point
β βββ package.json # React dependencies
β βββ vite.config.js # Vite configuration
β
βββ angular-app/ # Angular popup form
β βββ src/
β β βββ app/
β β β βββ app.component.ts # Form component logic
β β β βββ app.component.html # Form template
β β β βββ app.component.css # Form styling
β β βββ main.ts # Angular entry point
β βββ package.json # Angular dependencies
β βββ angular.json # Angular configuration
β
βββ README.md # This documentation
- Node.js (v18 or higher)
- npm or yarn package manager
React App:
cd react-app
npm install
Angular App:
cd angular-app
npm install
Terminal 1 - React App:
cd react-app
npm run dev
Runs on: http://localhost:5173/
Terminal 2 - Angular App:
cd angular-app
ng serve --port 4200
Runs on: http://localhost:4200/
sequenceDiagram
participant User
participant React
participant Angular
User->>React: Click "Create New Todo"
React->>Angular: Opens popup window
User->>Angular: Fill form and submit
Angular->>React: Send data via postMessage
React->>React: Update todo list
Angular->>Angular: Close popup window
Message Structure:
{
type: 'TODO_DATA', // Message type identifier
data: {
title: string, // Todo title
description: string, // Todo description
priority: 'low'|'medium'|'high', // Priority level
isEdit: boolean, // Creation vs Edit flag
id?: number // Todo ID (for edits)
},
source: 'angular-app' // Source identification
}
Security Features:
- Origin verification (
http://localhost:4200
) - Message type validation
- Data sanitization
Popup Lifecycle:
- Open - React opens Angular popup with window.open()
- Monitor - Heartbeat system tracks window status
- Communicate - Data exchange via postMessage
- Close - Automatic cleanup and state updates
Closure Detection:
- Periodic window.closed checking
- User abandonment detection
- Proper cleanup and status messages
- Clean Todo List - Organized display with hover effects
- Priority Indicators - Color-coded badges (π΄ High, π Medium, π’ Low)
- Timestamps - Creation and last updated times
- Interactive Elements - Click-to-edit with visual feedback
- Reactive Forms - Built-in validation and error handling
- Auto-fill Support - Pre-populated data for editing
- Clean Design - Focused form interface
- Responsive Layout - Works on different screen sizes
// Origin verification
if (event.origin !== 'http://localhost:4200') return
// Message type validation
if (event.data?.type !== 'TODO_DATA') return
// Data structure validation
const { title, description, priority } = event.data.data
if (!title || !description || !priority) return
- β Origin checking for all messages
- β Data structure validation
- β Error boundary handling
- β Secure window management
- β Input sanitization
- Open React app in browser (http://localhost:5173/)
- Click "Create New Todo (Angular)" button
- Fill out form in popup window
- Click "Submit" - popup closes, data appears in React
- Click on any existing todo to edit it
- Use delete buttons to remove todos
- β Popup window closed without submitting
- β Invalid form data handling
- β Network communication errors
- β Duplicate window prevention
- β Browser popup blockers
- Update origin URLs for production domains
- Configure proper CORS policies
- Implement HTTPS for secure communication
- Add error logging and monitoring
// Development
const ANGULAR_URL = 'http://localhost:4200'
const REACT_ORIGIN = 'http://localhost:5173'
// Production
const ANGULAR_URL = 'https://your-angular-app.com'
const REACT_ORIGIN = 'https://your-react-app.com'
- React 19 - Component framework
- Vite - Build tool and dev server
- Modern JavaScript - ES6+ features
- CSS3 - Modern styling with flexbox/grid
- Angular 20 - Framework
- Angular CLI - Build and dev tools
- Reactive Forms - Form handling
- TypeScript - Type-safe development
- PostMessage API - Cross-origin messaging
- Window API - Popup management
- Event Handling - Message processing
This project serves as a reference implementation. To extend or modify:
- Add New Message Types - Extend the communication protocol
- Enhanced Validation - Add more form validation rules
- Data Persistence - Add backend API integration
- Authentication - Implement user authentication flow
- Testing - Add unit and integration tests
This project is licensed under the MIT License - see the LICENSE file for details.
Built with β€οΈ using React and Angular by Armel Fogue