.NET - Websocket.On()

Register a handler for connections, disconnections, or messages for the websocket.

using Nitric.Sdk;
using Nitric.Sdk.Function;
var websocket = Nitric.Websocket("public");
websocket.On(WebsocketEventType.Connected, ctx => {
// handle connections
return ctx;
});
websocket.On(WebsocketEventType.Disconnected, ctx => {
// handle disconnections
return ctx;
});
websocket.On(WebsocketEventType.Message, ctx => {
// handle message
return ctx;
});
Nitric.Run();

Parameters

  • Name
    eventType
    Required
    Required
    Type
    WebsocketEventType
    Description

    The type of websocket event to listen for. Can be Connected, Disconnected, or Message.

  • Name
    middleware
    Required
    Required
    Type
    WebsocketMiddleware
    Description

    The middleware function to use as the handler for Websocket events.

Examples

Register a handler for message events

Messages can be read as literal strings using ctx.Req.Message().

websocket.On(WebsocketEventType.Message, ctx => {
var message = ctx.Req.Message();
Console.WriteLine($"New Message from {ctx.Req.ConnectionId}: {message}");
return ctx;
});

Or they can be serialized to a class type using ctx.Req.Message<ClassName>().

class EventUpdate
{
public float Status { get; set; }
public string Content { get; set; }
}
websocket.On(WebsocketEventType.Message, ctx => {
var message = ctx.Req.Message<EventUpdate>();
Console.WriteLine($"Status: {message.Status}, Message: {message.Content}")
return ctx;
});

Manage Websocket connections

To store Websocket connections you can use a Nitric collection.

using System;
using Nitric.Sdk.Function;
using Nitric.Sdk.Resource;
class OpenConnection
{
public string Id { get; set; }
public Dictionary<string, string> Metadata { get; set; }
}
var websocket = Nitric.Websocket("public");
var collections = Nitric.Collection<OpenConnection>("connections").With(
CollectionPermission.Writing, CollectionPermission.Deleting
);
websocket.On(WebsocketEventType.Connected, ctx =>
{
collections.Doc(ctx.Req.ConnectionId).Set(new OpenConnection
{
Id = ctx.Req.ConnectionId,
Metadata = new Dictionary<string, string> { } // store any metadata related to the connection here
});
return ctx;
});
websocket.On(WebsocketEventType.Disconnected, ctx =>
{
collections.Doc(ctx.Req.ConnectionId).Delete();
return ctx;
});
Nitric.Run();

Chain functions as a single method handler

using System;
using Nitric.Sdk.Function;
class Application
{
static WebsocketContext ValidateRequest(WebsocketContext ctx, Func<WebsocketContext, WebsocketContext> next)
{
// Validate Request
return next(ctx);
}
static WebsocketContext HandleRequest(WebsocketContext ctx, Func<WebsocketContext, WebsocketContext> next)
{
// Handle Request
return next(ctx);
}
public static void Main(string[] args) {
var websocket = Nitric.Websocket("public");
websocket.On(WebsocketNotificationType.Message, ValidateRequest, HandleRequest);
Nitric.Run();
}
}
Last updated on Oct 11, 2024