GitHunt
JI

JialinXin/wps-function-poc

Azure WebJobs Web PubSub client library for .NET

This extension provides data models using in communication with Web PubSub Service.

Getting started

Install the package

Install the package with NuGet:

dotnet add package Microsoft.Azure.WebPubSub.Common

Prerequisites

You must have an Azure subscription and an Azure resource group with a Web PubSub resource. Follow this step-by-step tutorial to create an Azure Web PubSub instance.

Key concepts

Using Web PubSub input binding

Please follow the input binding tutorial to learn about using this extension for building WebPubSubConnection to create Websockets connection to service with input binding.

Using Web PubSub output binding

Please follow the output binding tutorial to learn about using this extension for publishing Web PubSub messages.

Using Web PubSub trigger

Please follow the trigger binding tutorial to learn about triggering an Azure Function when an event is sent from service upstream.

Examples

Functions that uses Web PubSub input binding

[FunctionName("WebPubSubInputBindingFunction")]
public static WebPubSubConnection Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
    [WebPubSubConnection(Hub = "simplechat", UserId = "{query.userid}")] WebPubSubConnection connection)
{
    Console.WriteLine("login");
    return connection;
}

Functions that uses Web PubSub output binding

[FunctionName("WebPubSubOutputBindingFunction")]
public static async Task RunAsync(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
    [WebPubSub(Hub = "simplechat")] IAsyncCollector<WebPubSubOperation> operation)
{
    await operation.AddAsync(new SendToAll
    {
        Message = BinaryData.FromString("Hello Web PubSub"),
        DataType = MessageDataType.Text
    });
}

Functions that uses Web PubSub trigger

[FunctionName("WebPubSubTriggerFunction")]
public static async Task<MessageResponse> RunAsync(
    [WebPubSubTrigger("message", WebPubSubEventType.User)] 
    ConnectionContext context,
    string message,
    MessageDataType dataType)
{
    Console.WriteLine($"Request from: {context.userId}");
    Console.WriteLine($"Request message: {message}");
    Console.WriteLine($"Request message DataType: {dataType}");
    return new MessageResponse
    {
        Message = BinaryData.FromString("ack"),
    };
}

Troubleshooting

Please refer to Monitor Azure Functions for troubleshooting guidance.

Next steps

Read the introduction to Azure Function or creating an Azure Function guide.

Contributing

See our CONTRIBUTING.md for details on building,
testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution. For
details, visit cla.microsoft.com.

This project has adopted the Microsoft Open Source Code of Conduct.
For more information see the Code of Conduct FAQ
or contact opencode@microsoft.com with any
additional questions or comments.

Impressions