<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by RandyLahey on Medium]]></title>
        <description><![CDATA[Stories by RandyLahey on Medium]]></description>
        <link>https://medium.com/@wing-bok-freezing?source=rss-e914855d9651------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HyoHSPDRhUX-wAhhwc7hVA.png</url>
            <title>Stories by RandyLahey on Medium</title>
            <link>https://medium.com/@wing-bok-freezing?source=rss-e914855d9651------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 07:19:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wing-bok-freezing/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building Real-Time App in .NET with REPR]]></title>
            <link>https://medium.com/@wing-bok-freezing/building-real-time-apps-in-net-just-got-easier-withwebsockets-otp-f4b6e99bf230?source=rss-e914855d9651------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4b6e99bf230</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[real-time-communication]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[repr]]></category>
            <category><![CDATA[signalr]]></category>
            <dc:creator><![CDATA[RandyLahey]]></dc:creator>
            <pubDate>Wed, 14 Oct 2026 07:25:00 GMT</pubDate>
            <atom:updated>2026-10-14T09:00:52.281Z</atom:updated>
            <content:encoded><![CDATA[<p>WebSockets.Otp is inspired by REPR (Request-Endpoint-Response) principles and trying to bring the same elegance from Minimal APIs, FastEndpoints, or MediatR to real-time communication.</p><p>Let me show you how to build a real-time chat app in under 10 minutes.</p><h3><strong>Getting Started</strong></h3><p><strong>Step 1: Install the Package</strong></p><pre>dotnet add package WebSockets.Otp.Core</pre><p><strong>Step 2: Define Your First Endpoint</strong></p><p>The magic of WebSockets.Otp is how naturally endpoints map to message handlers:</p><pre>[WsEndpoint(&quot;chat/message/send&quot;)]<br>public class ChatEndpoint : WsEndpoint&lt;ChatMessage&gt;<br>{<br>    public override async Task HandleAsync(ChatMessage message, EndpointContext context)<br>    {<br>        await context.Send<br>            .Group(&quot;general-chat&quot;)<br>            .SendAsync(&quot;chat/message/receive&quot;, message, default);<br>    }<br>}<br></pre><pre>public class ChatMessage<br>{<br>    public string Username { get; set; }<br>    public string Message { get; set; }<br>}</pre><p>That’s it. No base classes to wrestle with, no complex configuration. Just a class with a HandleAsync method.</p><p><strong>Step 3: Wire It Up</strong></p><p>In your Program.cs:</p><pre>builder.Services.AddWsEndpoints();<br><br>app.MapEndpoints(&quot;/ws&quot;, (opt) =&gt;<br>{<br>    opt.OnConnected = async (context) =&gt;<br>    {<br>        await context.Groups.AddAsync(&quot;general-chat&quot;, context.ConnectionId);<br>    };<br>    <br>    opt.OnDisconnected = async (context) =&gt;<br>    {<br>        await context.Groups.RemoveAsync(&quot;general-chat&quot;, context.ConnectionId);<br>    };<br>})</pre><p>Three lines of configuration. Your WebSocket server is ready.</p><h3>The Client Side</h3><p>Install the JavaScript client:</p><pre>npm i websockets.otp.js</pre><p>Use it:</p><pre>const client = useWsClient({<br>    url: &quot;ws://localhost:5096/ws&quot;<br>});<br><br>await client.connect();<br><br>type Message = { username: string, message: string };<br><br>// Listen for messages<br>const off = client.receive&lt;Message&gt;(&#39;chat/message/receive&#39;, (msg) =&gt; {<br>    console.log(`${msg.username}: ${msg.message}`);<br>});<br><br>// Send a message<br>await client.notify&lt;Message&gt;(&#39;chat/message/send&#39;, {<br>    username: &quot;user1&quot;,<br>    message: &quot;Hello, WebSockets.Otp!&quot;<br>});</pre><p>The client mirrors the server’s endpoint-based approach. Your message names are your routes.</p><h3>Three Endpoint Patterns</h3><p>WebSockets.Otp supports three patterns, each for different use cases:</p><h3>1. Fire-and-Forget (No Request/Response)</h3><p>Perfect for telemetry, logging, or one-way notifications:</p><pre>[WsEndpoint(&quot;system/status&quot;)]<br>public class SystemStatusEndpoint : WsEndpoint<br>{<br>    public override async Task HandleAsync(EndpointContext context)<br>    {<br>        var buffer = context.Payload.Span;<br>        // Process raw data if send any<br>    }<br>}</pre><h3>2. Request-Only</h3><p>When you need to receive data but don’t need to respond directly:</p><pre>[WsEndpoint(&quot;notifications/subscribe&quot;)]<br>public class SubscribeEndpoint : WsEndpoint&lt;SubscribeRequest&gt;<br>{<br>    public override async Task HandleAsync(SubscribeRequest request, EndpointContext context)<br>    {<br>        await context.Groups.AddAsync(&quot;notifications&quot;, context.ConnectionId);<br>        <br>        await context.Send.SendAsync(&quot;notifications/subscribed&quot;, new<br>        {<br>            ConnectionId = context.ConnectionId,<br>            Data = &quot;Subscribed successfully&quot;<br>        }, default);<br>    }<br>}</pre><h3>3. Request/Response</h3><p>When you need a direct reply — like an RPC call over WebSockets:</p><pre>[WsEndpoint(&quot;calculator/add&quot;)]<br>public class AddEndpoint : WsEndpoint&lt;AddRequest, AddResponse&gt;<br>{<br>    public override async Task&lt;AddResponse&gt; HandleAsync(AddRequest request, EndpointContext context)<br>    {<br>        return new AddResponse<br>        {<br>            Result = request.A + request.B,<br>            Operation = &quot;addition&quot;<br>        };<br>    }<br>}</pre><pre>    type Request = { a: number, b: number }<br>    type Response = {<br>        result: number,<br>        operation: string<br>    }<br><br>    const result = await client.send&lt;Request, Response&gt;(&#39;calculator/add&#39;, { a: 2, b: 2 })<br><br>    console.log(`${result.operation}: ${result.result}`)</pre><p>This gives you HTTP-like request/response semantics over a persistent connection.</p><h3>Real-World Features</h3><h3>Dependency Injection Done Right</h3><p>Your endpoints are first-class citizens in the DI container:</p><pre>[WsEndpoint(&quot;auth/validate&quot;, ServiceLifetime.Singleton)]<br>public class AuthEndpoint : WsEndpoint&lt;AuthRequest, AuthResponse&gt;<br>{<br>    private readonly IAuthService _authService;<br>    private readonly ILogger&lt;AuthEndpoint&gt; _logger;<br><br>    public AuthEndpoint(IAuthService authService, ILogger&lt;AuthEndpoint&gt; logger)<br>    {<br>        _authService = authService;<br>        _logger = logger;<br>    }<br><br>    public override async Task&lt;AuthResponse&gt; HandleAsync(AuthRequest request, EndpointContext context)<br>    {<br>        var isValid = await _authService.ValidateAsync(request.Token);<br>        return new AuthResponse { Success = isValid };<br>    }<br>}</pre><p>Control the lifetime per endpoint. Inject services. Log everything.</p><h3>Group Management</h3><p>Building a chat room, live dashboard, or collaborative tool? Groups are built in:</p><pre>[WsEndpoint(&quot;chat/message/send&quot;)]<br>public class ChatEndpoint : WsEndpoint&lt;ChatMessage&gt;<br>{<br>    public override async Task HandleAsync(ChatMessage request, EndpointContext context)<br>    {<br>        // Join a room<br>        await context.Groups.AddAsync(&quot;chat-room&quot;, context.ConnectionId);<br>        <br>        // Send to one group<br>        await context.Send<br>            .Group(&quot;chat-room&quot;)<br>            .SendAsync(&quot;chat/message/receive&quot;, new { Message = &quot;Welcome!&quot; });<br><br>        // Send to multiple groups at once<br>        await context.Send<br>            .Group(&quot;chat-room&quot;)<br>            .Group(&quot;announcements&quot;)<br>            .SendAsync(&quot;chat/message/receive&quot;, new { Message = &quot;Important update!&quot; });<br><br>        // Leave a room<br>        await context.Groups.RemoveAsync(&quot;chat-room&quot;, context.ConnectionId);<br>    }<br>}</pre><p>No need to build your own connection registry. It’s handled for you.</p><h3>Authorization</h3><p>Apply authorization globally, per-endpoint, or both:</p><p><strong>Global (applies to all endpoints under the path):</strong></p><pre>app.MapEndpoints(&quot;/ws&quot;, (opt) =&gt;<br>{<br>    opt.AuthorizationData =<br>    [<br>        new AuthorizeAttribute<br>        {<br>            AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme<br>        }<br>    ];<br>});</pre><p><strong>Per-endpoint:</strong></p><pre>[Authorize(Policy = &quot;ws.chat&quot;)]<br>[WsEndpoint(&quot;chat/message/send&quot;)]<br>public class ChatEndpoint : WsEndpoint&lt;ChatMessage&gt;<br>{<br>    // ...<br>}</pre><p>Global rules for connection-wide concerns, endpoint attributes for fine-grained policies. They compose naturally.</p><h3>Scaling with Redis</h3><p>When you need to scale beyond a single server, WebSockets.Otp has you covered:</p><pre>dotnet add package WebSockets.Otp.Redis</pre><p>Setup is two lines:</p><pre>builder.Services.AddSingleton&lt;IConnectionMultiplexer&gt;(<br>    _ =&gt; ConnectionMultiplexer.Connect(&quot;localhost:6379&quot;));<br>builder.Services.AddRedisManager();</pre><p>Now your connection registry is shared across instances. A message sent to a group on Server A reaches clients connected to Server B.</p><h3>What’s on the Roadmap?</h3><p>The library is actively evolving. Coming soon:</p><ul><li>Performance optimizations</li><li>Client reconnection handling</li><li>More protocols support</li><li>Pre/Post processors</li><li>Rate limiting</li><li>Versioning</li></ul><p><strong>Links:</strong></p><ul><li>GitHub: <a href="https://github.com/VladUl287/WebSoket.Otp">https://github.com/VladUl287/WebSoket.Otp</a></li><li>NuGet: <a href="https://www.nuget.org/packages/WebSockets.Otp.Core/">https://www.nuget.org/packages/WebSockets.Otp.Core</a></li><li>npm: <a href="https://www.npmjs.com/package/websockets.otp.js">https://www.npmjs.com/package/websockets.otp.js</a></li></ul><p>Have questions or feedback? Drop a comment below or open an issue on GitHub. If you find WebSockets.Otp useful, a ⭐ on GitHub!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4b6e99bf230" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>