Microsoft.Azure.WebJobs.Extensions.WebPubSub 1.3.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET Standard 2.0
dotnet add package Microsoft.Azure.WebJobs.Extensions.WebPubSub --version 1.3.0
NuGet\Install-Package Microsoft.Azure.WebJobs.Extensions.WebPubSub -Version 1.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.WebPubSub" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Azure.WebJobs.Extensions.WebPubSub --version 1.3.0
#r "nuget: Microsoft.Azure.WebJobs.Extensions.WebPubSub, 1.3.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.Azure.WebJobs.Extensions.WebPubSub as a Cake Addin
#addin nuget:?package=Microsoft.Azure.WebJobs.Extensions.WebPubSub&version=1.3.0

// Install Microsoft.Azure.WebJobs.Extensions.WebPubSub as a Cake Tool
#tool nuget:?package=Microsoft.Azure.WebJobs.Extensions.WebPubSub&version=1.3.0

Azure WebJobs Web PubSub client library for .NET

This extension provides functionality for receiving Web PubSub webhook calls in Azure Functions, allowing you to easily write functions that respond to any event published to Web PubSub.

Source code | Package | API reference documentation | Product documentation | Samples

Getting started

Install the package

Install the Web PubSub extension with NuGet:

dotnet add package Microsoft.Azure.WebJobs.Extensions.WebPubSub

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.

Authenticate the client

In order to let the extension work with Azure Web PubSub service, you will need to provide a valid ConnectionString.

You can find the Keys for you Azure Web PubSub service in the Azure Portal.

The AzureWebJobsStorage connection string is used to preserve the processing checkpoint information as required refer to Storage considerations

For the local development use the local.settings.json file to store the connection string, <connection-string> can be set to WebPubSubConnectionString as default supported in the extension, or you can set customized names by mapping it with Connection = <connection-string> in function binding attributes:

{
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "<connection-string>": "Endpoint=https://<webpubsub-name>.webpubsub.azure.com;AccessKey=<access-key>;Version=1.0;"
  }
}

When deployed use the application settings to set the connection string.

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.

In Connect and UserEvent events, function will respect return values to send back service. Then service will depend on the response to proceed the request or else. The responses and events are paired. For example, Connect will only respect ConnectEventResponse or EventErrorResponse, and ignore other returns. When EventErrorResponse is returned, service will drop client connection. Please follow the trigger binding return value tutorial to learn about using the trigger return value.

Examples

Functions that uses Web PubSub input binding

public static class WebPubSubConnectionBindingFunction
{
    [FunctionName("WebPubSubConnectionBindingFunction")]
    public static WebPubSubConnection Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
        [WebPubSubConnection(Hub = "hub", UserId = "{query.userid}", Connection = "<connection-string>")] WebPubSubConnection connection)
    {
        Console.WriteLine("login");
        return connection;
    }
}

Functions that uses Web PubSub output binding

public static class WebPubSubOutputBindingFunction
{
    [FunctionName("WebPubSubOutputBindingFunction")]
    public static async Task RunAsync(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
        [WebPubSub(Hub = "hub", Connection = "<connection-string>")] IAsyncCollector<WebPubSubAction> action)
    {
        await action.AddAsync(WebPubSubAction.CreateSendToAllAction("Hello Web PubSub!", WebPubSubDataType.Text));
    }
}

Functions that uses Web PubSub trigger

public static class WebPubSubTriggerFunction
{
    [FunctionName("WebPubSubTriggerFunction")]
    public static void Run(
        ILogger logger,
        [WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] UserEventRequest request,
        string data,
        WebPubSubDataType dataType)
    {
        logger.LogInformation("Request from: {user}, data: {data}, dataType: {dataType}",
            request.ConnectionContext.UserId, data, dataType);
    }
}

Functions that uses Web PubSub trigger return value

public static class WebPubSubTriggerReturnValueFunction
{
    [FunctionName("WebPubSubTriggerReturnValueFunction")]
    public static UserEventResponse Run(
        [WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] UserEventRequest request)
    {
        return request.CreateResponse(BinaryData.FromString("ack"), WebPubSubDataType.Text);
    }
}

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.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 6,413 11/11/2022
1.2.0 26,241 3/10/2022
1.1.0 9,538 12/9/2021
1.0.0 2,559 11/12/2021
1.0.0-beta.4 1,068 10/28/2021
1.0.0-beta.3 3,381 7/26/2021
1.0.0-beta.2 315 7/15/2021
1.0.0-beta.1 3,380 4/27/2021