UiRealTimeCommunicator.TypeScriptGenerator-develop 1.0.20250216.2

dotnet tool install --global UiRealTimeCommunicator.TypeScriptGenerator-develop --version 1.0.20250216.2                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local UiRealTimeCommunicator.TypeScriptGenerator-develop --version 1.0.20250216.2                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=UiRealTimeCommunicator.TypeScriptGenerator-develop&version=1.0.20250216.2                
nuke :add-package UiRealTimeCommunicator.TypeScriptGenerator-develop --version 1.0.20250216.2                

UiRealTimeCommunicator

UiRealTimeCommunicator is a NuGet library designed to enable seamless strongly-typed message exchange between a C# .NET 8 server-side application and a TypeScript client-side application using SignalR. This library simplifies WebSocket-based communication by providing strict type safety and an intuitive API, making it easy to implement real-time features like live updates, notifications, and interactive communication.

🚀 Features:

  • 🏗 Strongly-typed messages: Enforces compile-time safety and ensures that communication between client and server is consistent.
  • 🧑‍💻 Code generation: Automatically generates TypeScript models and contracts from the server-side code, reducing boilerplate and ensuring synchronization between client and server code.
  • 🔐 Type safety on both sides: Guarantees that the TypeScript client-side code mirrors the server-side contracts, eliminating runtime errors due to mismatched data structures.
  • 📡 WebSocket communication: Uses SignalR to enable fast and reliable real-time communication over WebSockets.
  • 🔄 Automatic serialization/deserialization: Automatically serializes and deserializes messages, allowing developers to focus on business logic rather than manually handling data transformations.
  • ⚙️ Flexible and extensible: Supports various real-time messaging scenarios, including notifications, data streaming, and event-driven architectures.

🛠 Installation:

Server-Side (C# .NET 8)

Install the UiRealTimeCommunicator library via NuGet:
NuGet

dotnet add package UiRealTimeCommunicator

Additionally, install the CLI tool for generating TypeScript code:
NuGet

dotnet tool install --global UiRealTimeCommunicator.TypeScriptGenerator
# or update to the latest version
dotnet tool update --global UiRealTimeCommunicator.TypeScriptGenerator
Client-Side (TypeScript)

Once you have generated the TypeScript code, you will be able to use the contract and subscription models in your TypeScript frontend.

💻 Usage Example:

Server-Side (C#)

In your Program.cs, configure and add the UiRealTimeCommunicator to your services:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddUiRealTimeCommunicator(); // Add the service to DI container
...
var app = builder.Build();
app.UseUiRealTimeCommunicator(); // Enable real-time communication in the app pipeline

Define a SignalR Hub for communication:

[UiRtcHub("Weather")] // Declare a SignalR Hub with a specific name (Weather)
public class WeatherHub : IUiRtcHub { }

Define the sender contract for sending messages to the frontend:

public interface WeatherChannelSenderContract: IUiRtcSenderContract<WeatherHub>
{
    // Declare method and data for sending message to frontend
    [UiRtcMethod("WeatherForecast")]
    Task SendWeatherForecastAsync(WeatherForecastModel forecast);
}

In your service, use the sender contract to send messages:

public class WeatherService(IUiRtcSenderService senderService)
{
    public async Task WeatherServiceMethod(WeatherForecastModel model)
    {
        // Send message to frontend with strongly-typed contract
        await senderService.Send<WeatherChannelSenderContract>().SendWeatherForecastAsync(model);
    }
}

Define the handler contract to receive messages from the frontend:

[UiRtcMethod("GetWeatherForecast")]
public class GetWeatherForecastHandler() : IUiRtcHandler<WeatherHub, WeatherForecastRequestModel>
{
    public async Task ConsumeAsync(WeatherForecastRequestModel model)
    {
        // Handle message from frontend
        // Process the incoming request
    }
}

Define your data models and mark them for TypeScript code generation:

[TranspilationSource]
public class WeatherForecastModel
{
    public string City { get; set; }
    public double Temperature { get; set; }
    // Add additional fields
}

[TranspilationSource]
public class WeatherForecastRequestModel
{
    public string City { get; set; }
}

We utilize Tapper for model generation. For more details, visit the Tapper GitHub repository (external link).

Generate TypeScript Code for Client-Side

Use the CLI tool to generate TypeScript models and contracts from your C# project:

# -p Path to the project file (Xxx.csproj)
# -o Output directory and file
dotnet-uirtc -p ".\Examples\Simple\App-backend\App-backend.csproj" -o ".\Examples\Simple\app-frontend\src\communication\contract.ts"
Client-Side (TypeScript)

Install SignalR

Install SignalR in your TypeScript project. You can find more details on the SignalR npm page (external link).

npm install @microsoft/signalr
# or
yarn add @microsoft/signalr

In the TypeScript client, initialize the SignalR connection:

import { uiRtc } from "./communication/contract.ts";

await uiRtc.initAsync({
  serverUrl: "http://localhost:5064/", // Your server URL
  activeHubs: "All", // Specify which hubs to subscribe to
});

Send a message from the frontend:

import {
  uiRtcCommunication,
  WeatherForecastRequestModel,
} from "../../communication/contract";

// Call a backend method and send a strongly-typed model
await uiRtcCommunication.Weather.GetWeatherForecast({
  city: "Kharkiv",
} as WeatherForecastRequestModel); // Strongly typed

Subscribe to a message and handle the response:

import {
  uiRtcSubscription,
  WeatherForecastModel,
} from "../../communication/contract";

// Listen for messages from the backend
uiRtcSubscription.Weather.WeatherForecast(
  (data: WeatherForecastModel) => {
    // Handle received data
    console.log("Weather data received: ", data);
  }
);

🎯 Why UiRealTimeCommunicator?

  • ✅ Easy setup and integration: Simple installation and configuration for both server and client sides. No complicated setup or dependencies.
  • ✅ Reliable data exchange: Ensures high-quality, real-time data transfer using WebSockets and SignalR with strong type safety.
  • ✅ Fully typed communication: Both client and server-side code are strongly typed, reducing runtime errors and improving developer productivity.
  • ✅ Automatic code generation: The CLI tool automatically generates TypeScript models, ensuring consistency between server and client code and reducing boilerplate.
  • ✅ Scalable architecture: Ideal for building scalable real-time applications such as chat systems, live updates, and notifications.
  • ✅ Reduces boilerplate code: The framework abstracts away many manual tasks like serialization, deserialization, and message routing, allowing developers to focus on core functionality.

Start building real-time applications today! The library ensures a seamless and type-safe communication layer for your .NET and TypeScript applications.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
1.0.20250216.2 112 2/16/2025
1.0.20250215.2 117 2/15/2025
1.0.20250213.2 144 2/13/2025
1.0.20250212.2 118 2/12/2025
1.0.20250209.23 124 2/9/2025
1.0.20250209.21 123 2/9/2025
1.0.20250209.6 110 2/9/2025
1.0.20250209.4 102 2/9/2025
1.0.20250208.11 117 2/8/2025
1.0.20250208.10 108 2/8/2025
1.0.20250208.8 122 2/8/2025
1.0.20250208.7 120 2/8/2025
1.0.20250208.6 107 2/8/2025
1.0.20250208.5 111 2/8/2025
1.0.20250208.4 108 2/8/2025
1.0.20250208.3 118 2/8/2025
1.0.20250201.3 126 2/1/2025
1.0.20250131.6 135 1/31/2025
1.0.203-pr 89 2/16/2025
1.0.200-pr 110 2/15/2025
1.0.197-pr 103 2/13/2025
1.0.194-pr 101 2/12/2025
1.0.191-pr 107 2/9/2025
1.0.189-pr 95 2/9/2025
1.0.188-pr 101 2/9/2025
1.0.187-pr 101 2/9/2025
1.0.186-pr 92 2/9/2025
1.0.185-pr 97 2/9/2025
1.0.184-pr 96 2/9/2025
1.0.179-pr 110 2/9/2025
1.0.177-pr 103 2/9/2025
1.0.176-pr 103 2/9/2025
1.0.174-pr 107 2/9/2025
1.0.172-pr 100 2/9/2025
1.0.149 113 2/1/2025
1.0.148 108 1/31/2025
1.0.147 119 1/31/2025
1.0.145 133 1/31/2025