-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Web PubSub function bindings sdk. #20459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3beea43
6b8e262
d9445f7
1e77a0a
4c02f4d
6c313a9
116f650
5499373
86fa082
59675d8
175dd01
887dcb2
8e12180
bf4cd72
60d35e7
2d25612
90e3b62
f6cdb9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <PropertyGroup> | ||
| <IsClientLibrary>true</IsClientLibrary> | ||
| </PropertyGroup> | ||
|
|
||
| <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\, Directory.Build.props))\Directory.Build.props" /> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.30803.129 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.WebJobs.Extensions.WebPubSub", "src\Microsoft.Azure.WebJobs.Extensions.WebPubSub.csproj", "{6CF5F6B5-F8D6-4D92-9AE5-F766B1C83EED}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests", "tests\Microsoft.Azure.WebJobs.Extensions.WebPubSub.Tests.csproj", "{89759B66-5CF1-4A86-9D07-D3DA48300AE9}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {6CF5F6B5-F8D6-4D92-9AE5-F766B1C83EED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {6CF5F6B5-F8D6-4D92-9AE5-F766B1C83EED}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {6CF5F6B5-F8D6-4D92-9AE5-F766B1C83EED}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {6CF5F6B5-F8D6-4D92-9AE5-F766B1C83EED}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {89759B66-5CF1-4A86-9D07-D3DA48300AE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {89759B66-5CF1-4A86-9D07-D3DA48300AE9}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {89759B66-5CF1-4A86-9D07-D3DA48300AE9}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {89759B66-5CF1-4A86-9D07-D3DA48300AE9}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {AFE7981B-5322-4E4D-BACC-3380116A52F5} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # 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. | ||
|
|
||
| ## Getting started | ||
|
|
||
| ### Install the package | ||
|
|
||
| Install the Web PubSub extension with [NuGet][nuget]: | ||
|
|
||
| ```Powershell | ||
| dotnet add package Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| ``` | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| You must have an [Azure subscription](https://azure.microsoft.com/free/) and an Azure resource group with a Web PubSub resource. Follow this [step-by-step tutorial](https://review.docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance?branch=release-azure-web-pubsub) to create an Azure Web PubSub instance. | ||
|
|
||
| ## Key concepts | ||
|
|
||
| ### Using Web PubSub input binding | ||
|
|
||
| Please follow the [input binding tutorial](https://scaling-garbanzo-3fe86650.pages.github.io/references/functions-bindings#input-binding) 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](https://scaling-garbanzo-3fe86650.pages.github.io/references/functions-bindings#output-binding) to learn about using this extension for publishing Web PubSub messages. | ||
|
|
||
| ### Using Web PubSub trigger | ||
|
|
||
| Please follow the [trigger binding tutorial](https://scaling-garbanzo-3fe86650.pages.github.io/references/functions-bindings#trigger-binding) to learn about triggering an Azure Function when an event is sent from service upstream. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Functions that uses Web PubSub input binding | ||
|
|
||
| ```C# Snippet:WebPubSubInputBindingFunction | ||
| [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 | ||
|
|
||
| ```c# Snippet:WebPubSubOutputBindingFunction | ||
| [FunctionName("WebPubSubOutputBindingFunction")] | ||
| public static async Task RunAsync( | ||
| [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, | ||
| [WebPubSub(Hub = "simplechat")] IAsyncCollector<WebPubSubEvent> webpubsubEvent) | ||
| { | ||
| await webpubsubEvent.AddAsync(new WebPubSubEvent | ||
| { | ||
| Operation = Operation.SendToAll, | ||
| Message = new Message("Hello Web PubSub"), | ||
| DataType = MessageDataType.Text | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| ### Functions that uses Web PubSub trigger | ||
|
|
||
| ```C# Snippet:WebPubSubTriggerFunction | ||
| [FunctionName("WebPubSubTriggerFunction")] | ||
| public static async Task<MessageResponse> RunAsync( | ||
| [WebPubSubTrigger("message", EventType.User)] | ||
| ConnectionContext context, | ||
| WebPubSubMessage message, | ||
| MessageDataType dataType) | ||
| { | ||
| Console.WriteLine($"Request from: {context.userId}"); | ||
| Console.WriteLine($"Request message: {message.Body.ToString()}"); | ||
| Console.WriteLine($"Request message DataType: {dataType}"); | ||
| return new MessageResponse | ||
| { | ||
| Message = new WebPubSubMessage("ack"), | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| Please refer to [Monitor Azure Functions](https://docs.microsoft.com/azure/azure-functions/functions-monitoring) for troubleshooting guidance. | ||
|
|
||
| ## Next steps | ||
|
|
||
| Read the [introduction to Azure Function](https://docs.microsoft.com/azure/azure-functions/functions-overview) or [creating an Azure Function guide](https://docs.microsoft.com/azure/azure-functions/functions-create-first-azure-function). | ||
|
|
||
| ## Contributing | ||
|
|
||
| See our [CONTRIBUTING.md][contrib] 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][cla]. | ||
|
|
||
| This project has adopted the [Microsoft Open Source Code of Conduct][coc]. | ||
| For more information see the [Code of Conduct FAQ][coc_faq] | ||
| or contact [[email protected]][coc_contact] with any | ||
| additional questions or comments. | ||
|
|
||
|  | ||
|
|
||
| <!-- LINKS --> | ||
| [source]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/search/Microsoft.Azure.WebJobs.Extensions.WebPubSub/src | ||
| [package]: https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.WebPubSub/ | ||
| [docs]: https://docs.microsoft.com/dotnet/api/Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| [nuget]: https://www.nuget.org/ | ||
|
|
||
| [contrib]: https://github.com/Azure/azure-sdk-for-net/tree/master/CONTRIBUTING.md | ||
| [cla]: https://cla.microsoft.com | ||
| [coc]: https://opensource.microsoft.com/codeofconduct/ | ||
| [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
| [coc_contact]: mailto:[email protected] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] | ||
| public class ConnectResponse : ServiceResponse | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be public?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Customer need to return different response in different scenarios. See sample usage. |
||
| { | ||
| [JsonProperty(Required = Required.Default)] | ||
| public string UserId { get; set; } | ||
|
|
||
| [JsonProperty(Required = Required.Default)] | ||
| public string[] Groups { get; set; } | ||
|
|
||
| [JsonProperty(Required = Required.Default)] | ||
| public string Subprotocol { get; set; } | ||
|
|
||
| [JsonProperty(Required = Required.Default)] | ||
| public string[] Roles { get; set; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Collections.Generic; | ||
|
|
||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] | ||
| public class ConnectionContext | ||
| { | ||
| /// <summary> | ||
| /// The type of the message. | ||
| /// </summary> | ||
| public WebPubSubEventType EventType { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The event name of the message. | ||
| /// </summary> | ||
| public string EventName { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The hub which the message belongs to. | ||
| /// </summary> | ||
| public string Hub { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The connection-id of the client which send the message. | ||
| /// </summary> | ||
| public string ConnectionId { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The user identity of the client which send the message. | ||
| /// </summary> | ||
| public string UserId { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The signature for validation | ||
| /// </summary> | ||
| public string Signature { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The headers of request. | ||
| /// </summary> | ||
| public Dictionary<string, StringValues> Headers { get; internal set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] | ||
| public class ErrorResponse : ServiceResponse | ||
| { | ||
| [JsonProperty(Required = Required.Always)] | ||
| public WebPubSubErrorCode Code { get; set; } | ||
|
|
||
| [JsonProperty(Required = Required.Default)] | ||
| public string ErrorMessage { get; set; } | ||
|
|
||
| public ErrorResponse(WebPubSubErrorCode code, string message = null) | ||
| { | ||
| Code = code; | ||
| ErrorMessage = message; | ||
| } | ||
|
|
||
| [JsonConstructor] | ||
| public ErrorResponse() | ||
| { | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Runtime.Serialization; | ||
|
|
||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Converters; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| [JsonConverter(typeof(StringEnumConverter))] | ||
JialinXin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public enum MessageDataType | ||
| { | ||
| [EnumMember(Value = "binary")] | ||
| Binary, | ||
| [EnumMember(Value = "json")] | ||
| Json, | ||
| [EnumMember(Value = "text")] | ||
| Text | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] | ||
| public class MessageResponse : ServiceResponse | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be public? |
||
| { | ||
| [JsonProperty(Required = Required.Always)] | ||
| public WebPubSubMessage Message { get; set; } | ||
|
|
||
| public MessageDataType DataType { get; set; } = MessageDataType.Text; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| public abstract class ServiceResponse | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Reflection; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.WebPubSub | ||
| { | ||
| internal class WebPubSubAsyncCollector: IAsyncCollector<WebPubSubEvent> | ||
| { | ||
| private readonly IWebPubSubService _service; | ||
|
|
||
| internal WebPubSubAsyncCollector(IWebPubSubService service) | ||
| { | ||
| _service = service; | ||
| } | ||
|
|
||
| public async Task AddAsync(WebPubSubEvent item, CancellationToken cancellationToken = default) | ||
| { | ||
| if (item == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(item)); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var method = typeof(IWebPubSubService).GetMethod(item.Operation.ToString(), | ||
| BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); | ||
|
|
||
| var task = (Task)method.Invoke(_service, new object[] { item }); | ||
|
|
||
| await task.ConfigureAwait(false); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| throw new ArgumentException($"Not supported operation: {item.Operation}, exception: {ex}"); | ||
| } | ||
| } | ||
|
|
||
| public Task FlushAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.