Skip to content
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

[WebPubSubFunction] Add AWPS function samples. #22140

Merged
3 commits merged into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/src) |
[Package](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.WebPubSub) |
[API reference documentation](https://azure.github.io/azure-webpubsub/references/functions-bindings) |
[Product documentation](https://aka.ms/awps/doc) |
[Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebJobs.Extensions.WebPubSub/samples/)

## Getting started

### Install the package
Expand Down Expand Up @@ -56,61 +62,72 @@ In `Connect` and `Message` events, function will respect return values to send b

### Functions that uses Web PubSub input binding

```cs
[FunctionName("WebPubSubInputBindingFunction")]
public static WebPubSubConnection Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
[WebPubSubConnection(Hub = "simplechat", UserId = "{query.userid}")] WebPubSubConnection connection)
```C# Snippet:WebPubSubConnectionBindingFunction
public static class WebPubSubConnectionBindingFunction
{
Console.WriteLine("login");
return connection;
[FunctionName("WebPubSubConnectionBindingFunction")]
public static WebPubSubConnection Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
[WebPubSubConnection(Hub = "hub", UserId = "{query.userid}")] WebPubSubConnection connection)
{
Console.WriteLine("login");
return connection;
}
}
```

### Functions that uses Web PubSub output binding

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

### Functions that uses Web PubSub trigger

```cs
[FunctionName("WebPubSubTriggerFunction")]
public static void Run(
[WebPubSubTrigger("message", WebPubSubEventType.User)]
ConnectionContext context,
string message,
MessageDataType dataType)
```C# Snippet:WebPubSubTriggerFunction
public static class WebPubSubTriggerFunction
{
Console.WriteLine($"Request from: {context.userId}");
Console.WriteLine($"Request message: {message}");
Console.WriteLine($"Request message DataType: {dataType}");
[FunctionName("WebPubSubTriggerFunction")]
public static void Run(
ILogger logger,
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context,
string message,
MessageDataType dataType)
{
logger.LogInformation("Request from: {user}, message: {message}, dataType: {dataType}",
context.UserId, message, dataType);
}
}
```

### Functions that uses Web PubSub trigger return value

```cs
[FunctionName("WebPubSubTriggerReturnValueFunction")]
public static MessageResponse RunAsync(
[WebPubSubTrigger("message", WebPubSubEventType.User)] ConnectionContext context)
```C# Snippet:WebPubSubTriggerReturnValueFunction
public static class WebPubSubTriggerReturnValueFunction
{
return new MessageResponse
[FunctionName("WebPubSubTriggerReturnValueFunction")]
public static MessageResponse Run(
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context)
{
Message = BinaryData.FromString("ack"),
DataType = MessageDataType.Text
};
return new MessageResponse
{
Message = BinaryData.FromString("ack"),
DataType = MessageDataType.Text
};
}
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;

namespace Microsoft.Azure.WebJobs.Samples
{
#region Snippet:WebPubSubConnectionBindingFunction
public static class WebPubSubConnectionBindingFunction
{
[FunctionName("WebPubSubConnectionBindingFunction")]
public static WebPubSubConnection Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
[WebPubSubConnection(Hub = "hub", UserId = "{query.userid}")] WebPubSubConnection connection)
{
Console.WriteLine("login");
return connection;
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;

namespace Microsoft.Azure.WebJobs.Samples
{
#region Snippet:WebPubSubOutputBindingFunction
public static class WebPubSubOutputBindingFunction
{
[FunctionName("WebPubSubOutputBindingFunction")]
public static async Task RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
[WebPubSub(Hub = "hub")] IAsyncCollector<WebPubSubOperation> operation)
{
await operation.AddAsync(new SendToAll
{
Message = BinaryData.FromString("Hello Web PubSub"),
DataType = MessageDataType.Text
});
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Samples
{
#region Snippet:WebPubSubTriggerFunction
public static class WebPubSubTriggerFunction
{
[FunctionName("WebPubSubTriggerFunction")]
public static void Run(
ILogger logger,
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context,
string message,
MessageDataType dataType)
{
logger.LogInformation("Request from: {user}, message: {message}, dataType: {dataType}",
context.UserId, message, dataType);
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;

namespace Microsoft.Azure.WebJobs.Samples
{
#region Snippet:WebPubSubTriggerReturnValueFunction
public static class WebPubSubTriggerReturnValueFunction
{
[FunctionName("WebPubSubTriggerReturnValueFunction")]
public static MessageResponse Run(
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] ConnectionContext context)
{
return new MessageResponse
{
Message = BinaryData.FromString("ack"),
DataType = MessageDataType.Text
};
}
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter"/>
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>

<ItemGroup>
Expand Down