Skip to content

dotnetdreamer/capacitor-signalr-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Capacitor SignalR Demo

A complete real-time chat application demonstrating the Capacitor SignalR Plugin in action.

recording.mp4

🌟 What's Included

  • ASP.NET Core SignalR Backend - Real-time hub implementation
  • Ionic Angular Mobile App - Cross-platform mobile client
  • Web Test Client - Browser-based testing interface

πŸš€ Quick Start

Prerequisites

1. Clone the Repository

git clone https://github.com/dotnetdreamer/capacitor-signalr-demo.git
cd capacitor-signalr-demo

2. Set up the SignalR Backend

Option 1: Using .NET CLI (Recommended)

cd web
dotnet run

Option 2: Using Visual Studio

  1. Open web/web.sln in Visual Studio
  2. Set the web project as the startup project (right-click β†’ "Set as Startup Project")
  3. Press F5 or click the "Start" button to run the project
  4. Visual Studio will automatically open a browser - you can close it as we'll use the mobile app

The backend will start on http://localhost:5236 with a SignalR hub at /chatHub.

Keep this running - the backend needs to be active for the demo to work.

3. Set up the Mobile App

Open a new terminal and run:

cd mobile
npm install
npm run build or ionic capacitor run android --livereload --external --host=0.0.0.0
npx cap sync

Now go to home.page.ts and change the url to your ip address i.e inside setupSignalRConnection() e.g http://0.0.0.0:5236/chatHub to http://192.168.1.130:5236/chatHub

4. Configure the Connection URL

⚠️ Important: Update the SignalR hub URL with your computer's IP address.

  1. Find your computer's IP address:

    # Windows
    ipconfig
    
    # macOS/Linux
    ifconfig
  2. Edit mobile/src/app/home/home.page.ts and update the URL:

    const connectionInfo = await CapacitorSignalR.create({
      url: 'http://YOUR_IP_ADDRESS:5236/chatHub', // Replace with your IP
      // ... other options
    });

    Example:

    url: 'http://192.168.1.100:5236/chatHub',

5. Run the Mobile App

For Android:

ionic capacitor run android --livereload --external --host=0.0.0.0

For iOS:

ionic capacitor run ios --livereload --external --host=0.0.0.0

6. Test the Web Client (Optional)

Open the web-test.html file in your browser and:

  1. Update the hub URL to match your IP address
  2. Click "Connect"
  3. Subscribe to "ReceiveMessage" event
  4. Send messages to test cross-platform communication

πŸ“± Demo Features

Real-time Chat

  • βœ… Send and receive messages instantly
  • βœ… Cross-platform messaging (mobile ↔ web)
  • βœ… Message persistence during session
  • βœ… User identification

Connection Management

  • βœ… Connection state monitoring
  • βœ… Auto-reconnection with configurable delays
  • βœ… Connection error handling
  • βœ… Visual connection status indicators

Transport Protocols

  • βœ… WebSockets (preferred)
  • βœ… Server-Sent Events (fallback)
  • βœ… Long Polling (fallback)

Cross-platform Support

  • βœ… Android native implementation
  • βœ… iOS native implementation
  • βœ… Web fallback using Microsoft SignalR JS client

πŸ—οΈ Project Structure

capacitor-signalr-demo/
β”œβ”€β”€ web/                          # ASP.NET Core SignalR Backend
β”‚   β”œβ”€β”€ Hubs/ChatHub.cs          # SignalR Hub implementation
β”‚   β”œβ”€β”€ Program.cs               # Server configuration
β”‚   └── Controllers/             # Web controllers
β”‚
β”œβ”€β”€ mobile/                       # Ionic Angular Mobile App
β”‚   β”œβ”€β”€ src/app/home/            # Main chat interface
β”‚   β”œβ”€β”€ capacitor.config.ts      # Capacitor configuration
β”‚   └── package.json             # Dependencies
β”‚
└── web-test.html                # Standalone web test client

πŸ”§ Troubleshooting

Connection Issues

Problem: "Connection refused" or "Failed to connect"

  • βœ… Ensure the backend is running (dotnet run in the web folder)
  • βœ… Check the IP address in your mobile app configuration
  • βœ… Ensure your device and computer are on the same network
  • βœ… Check firewall settings (allow port 5236)

Mobile App Issues

Problem: App doesn't connect on device

  • βœ… Use your computer's IP address, not localhost
  • βœ… Ensure livereload is running with --external --host=0.0.0.0
  • βœ… Check that the device is on the same WiFi network

Problem: Messages appear twice

  • βœ… This has been fixed in the latest version
  • βœ… Ensure you're using the latest plugin version

Backend Issues

Problem: CORS errors

  • βœ… The backend is configured to allow all origins for demo purposes
  • βœ… Check Program.cs for CORS configuration

πŸ“š How It Works

SignalR Hub (Backend)

The ChatHub.cs implements the SignalR hub with methods:

  • SendMessage(user, message) - Broadcasts messages to all connected clients
  • OnConnectedAsync() - Handles new connections
  • OnDisconnectedAsync() - Handles disconnections

Mobile Client

The Ionic app uses the Capacitor SignalR plugin to:

  • Create and manage SignalR connections
  • Send messages through hub methods
  • Listen for real-time events
  • Handle connection state changes

Web Client

The web-test.html demonstrates direct usage of the Microsoft SignalR JavaScript client for comparison and testing.

🌐 Related Links

πŸ› Issues & Feedback

Found a bug or have a suggestion? Please open an issue on the main plugin repository: Report Issues

πŸ“„ License

This demo application is provided as-is for demonstration purposes. See the main plugin repository for licensing information.


Made with ❀️ for the Capacitor community

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published