Kanject.InstantMessaging

View .md

WebSocket-based chat with online and offline presence — one-to-one, group, and topic conversations. A serverless model: server-side managers own conversations and messages, and a WebSocket hub over API Gateway pushes them to connected clients, all in your own AWS account.

You'll learn
  • Let InstantMessaging create its tables and WebSocket API in your AWS account
  • Register the messaging services and attach the API Gateway WebSocket hub
  • Create a conversation and persist a message through the managers
  • Know the presence, delivery-report, and conversation-type surface

Provision

InstantMessaging creates its DynamoDB tables and indexes in your account when Setup is true, and the API Gateway chat hub creates its WebSocket API unless you set DisableCreateWebsocket and pass an existing API's ApiId and ConnectionEndpointUrl.

Register it in your service

Register the messaging services with AddServerlessInstantMessaging(options => …), and attach a real-time transport in its use: delegate with UseApiGatewayWebsocketChatHub(...). The transport is an API Gateway WebSocket hub. A connected client sends two actions: send_message to post a message and ping to keep its connection alive. The hub refuses any other action, so conversations are created server-side, through the managers below.

csharp
using Kanject.InstantMessaging.Provider.AwsV2.Extensions;using Kanject.InstantMessaging.ChatHub.Provider.ApiGateway.Extensions;​var builder = WebApplication.CreateBuilder(args);​// Register the messaging services; the use: delegate attaches the// WebSocket transport.builder.Services.AddServerlessInstantMessaging(    options =>    {        options.AwsRegion = appSettings.AwsRegion;        options.Namespace = appSettings.Stage;   // per-stage isolation    },    use: hub => hub.UseApiGatewayWebsocketChatHub(options: chatHub =>    {        chatHub.ConnectionEndpointUrl = appSettings.WebSocketEndpointUrl;    }));​var app = builder.Build();app.Run();

Create a conversation, send a message

There's no client object — you use the server-side managers. IConversationManager<T> creates conversations (OneOnOne, GroupChat, TopicChat); IConversationChatManager<T> persists messages; IHubPublisher.PublishMessageAsync<T> pushes them to connected participants:

csharp
// Server-side managers create conversations and persist messages;// IHubPublisher pushes them to connected clients over the WebSocket hub.public class SupportChat(    IConversationManager<Conversation> conversations,    IConversationChatManager<Conversation> chats,    IHubPublisher hub){    public async Task<string?> StartAsync(string userA, string userB, Guid actingUserId)    {        var convo = await conversations.AddConversationAsync(new AddConversationRequest        {            ConversationType   = ConversationType.OneOnOne,            ParticipantUserIds = new List<string> { userA, userB },        }, actingUserId);​        await chats.AddConversationChatAsync(new AddConversationChatRequest        {            /* conversationId, body, MessageType.Text, … */        }, actingUserId);​        return convo?.ConversationId;    }}

What you get

  • Conversation types — OneOnOne, GroupChat, TopicChat, one manager API.
  • Real-time over WebSocket — API Gateway hub with IHubPublisher push; the sender hears message_acknowledged once the hub accepts a message.
  • Presence — a user goes Online on connect and Offline on disconnect, per device (SessionStatus), and each change is published as an EventHub event.
  • Delivery reports — a delivery record per message and recipient (IDeliveryReport).
  • Message history — paginated backlog via GetConversationChatsAsync, readable only by the conversation's participants.
  • Message types — Text and Media.
Recap
  • Register AddServerlessInstantMessaging + UseApiGatewayWebsocketChatHub (in its use: delegate); Setup creates its tables, and the hub creates its WebSocket API.
  • Server-side managers (IConversationManager<T>, IConversationChatManager<T>) own conversations and messages.
  • Real-time push is IHubPublisher.PublishMessageAsync<T> over an API Gateway WebSocket hub.
  • Conversation types are OneOnOne / GroupChat / TopicChat; online/offline presence and delivery reports are built in.
Was this page helpful?