Skip to content

Commit 1bac8dc

Browse files
authored
Add a quick start sample
1 parent 595a81c commit 1bac8dc

File tree

6 files changed

+134
-13
lines changed

6 files changed

+134
-13
lines changed
5.89 KB
Loading
23.7 KB
Loading

articles/azure-web-pubsub/socketio-serverless-function-binding.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ For the local development, use the `local.settings.json` file to store the conne
4747
{
4848
"Values": {
4949
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
50-
"WebPubSubForSocketIOConnectionString": "Endpoint=https://<webpubsub-name>.webpubsub.azure.com;AccessKey=<access-key>;Version=1.0;"
50+
`WebPubSubForSocketIOConnectionString`: "Endpoint=https://<webpubsub-name>.webpubsub.azure.com;AccessKey=<access-key>;Version=1.0;"
5151
}
5252
}
5353
```
@@ -76,7 +76,7 @@ For the local development, use the `local.settings.json` file to store the conne
7676
}
7777
```
7878

79-
If you want to use identity based configuration and running online, the `AzureWebJobsStorage` should refer to [Connecting to host storage with an identity](../azure-functions/functions-reference.md#connecting-to-host-storage-with-an-identity)
79+
If you want to use identity based configuration and running online, the `AzureWebJobsStorage` should refer to [Connecting to host storage with an identity](../azure-functions/functions-reference.md#connecting-to-host-storage-with-an-identity).
8080

8181
## Input Binding
8282

@@ -101,7 +101,7 @@ The attribute for input binding is `[SocketIONegotiation]`.
101101
| Attribute property | Description |
102102
|---------|---------|
103103
| Hub | The hub name that a client needs to connect to. |
104-
| Connection | The name of the app setting that contains the Socket.IO connection string (defaults to "WebPubSubForSocketIOConnectionString"). |
104+
| Connection | The name of the app setting that contains the Socket.IO connection string (defaults to `WebPubSubForSocketIOConnectionString`). |
105105
| UserId | The userId of the connection. It applys to all sockets in the connection. It becomes the `sub` claim in the generated token. |
106106

107107
# [JavaScript Model v4](#tab/javascript-v4)
@@ -139,7 +139,7 @@ app.http('negotiate', {
139139
| direction | Must be `in` |
140140
| name | Variable name used in function code for input connection binding object |
141141
| hub | The hub name that a client needs to connect to. |
142-
| connection | The name of the app setting that contains the Socket.IO connection string (defaults to "WebPubSubForSocketIOConnectionString"). |
142+
| connection | The name of the app setting that contains the Socket.IO connection string (defaults to `WebPubSubForSocketIOConnectionString`). |
143143
| userId | The userId of the connection. It applys to all sockets in the connection. It becomes the `sub` claim in the generated token. |
144144

145145
---
@@ -211,7 +211,7 @@ The attribute for trigger binding is `[SocketIOTrigger]`.
211211

212212
### Binding Data
213213

214-
`[SocketIOTrigger]` binds some variable to binding data. You can learn more about it from [Azure Functions binding expression patterns](../azure-functions/functions-bindings-expressions-patterns.md)
214+
`[SocketIOTrigger]` binds some variables to binding data. You can learn more about it from [Azure Functions binding expression patterns](../azure-functions/functions-bindings-expressions-patterns.md)
215215

216216

217217

@@ -361,7 +361,7 @@ The attribute for input binding is `[SocketIO]`.
361361
| Attribute property | Description |
362362
|---------|---------|
363363
| Hub | The hub name that a client needs to connect to. |
364-
| Connection | The name of the app setting that contains the Socket.IO connection string (defaults to "WebPubSubForSocketIOConnectionString"). |
364+
| Connection | The name of the app setting that contains the Socket.IO connection string (defaults to `WebPubSubForSocketIOConnectionString`). |
365365

366366
# [JavaScript Model v4](#tab/javascript-v4)
367367

@@ -402,7 +402,7 @@ app.generic('newMessage', {
402402
|---------|---------|
403403
| type | Must be `socketio` |
404404
| hub | The hub name that a client needs to connect to. |
405-
| connection | The name of the app setting that contains the Socket.IO connection string (defaults to "WebPubSubForSocketIOConnectionString"). |
405+
| connection | The name of the app setting that contains the Socket.IO connection string (defaults to `WebPubSubForSocketIOConnectionString`). |
406406

407407
---
408408

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: 'Quickstart: Build chat app with Azure Function in Socket.IO Serverless Mode'
3+
description: In this artical, you will familiar with the samples of using Web PubSub for Socket.IO with Azure Function in Serverless Mode.
4+
keywords: Socket.IO, serverless, azure function, Socket.IO on Azure, multi-node Socket.IO, scaling Socket.IO, socketio, azure socketio
5+
author: zackliu
6+
ms.author: chenyl
7+
ms.date: 09/01/2024
8+
ms.service: azure-web-pubsub
9+
ms.topic: tutorial
10+
zone_pivot_groups: programming-languages-set-functions
11+
---
12+
13+
# Quickstart: Build chat app with Azure Function in Socket.IO Serverless Mode (Preview)
14+
15+
In this artical, you will use samples to familiar with how to build a chat app using Web PubSub for Socket.IO in Serverless Mode with Azure Function online and use Identity based authentication to secure your app.
16+
17+
The project source uses the Bicep to deploy the infrastructure in Azure and Azure Function Core Tools to deploy the code to the Function App.
18+
19+
## Prerequest
20+
21+
+ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
22+
23+
+ [Azure Functions Core Tools](functions-run-local.md#install-the-azure-functions-core-tools).
24+
25+
+ [.NET 8.0 SDK](https://dotnet.microsoft.com/download)
26+
27+
::: zone pivot="programming-language-typescript"
28+
+ [Node.js 18](https://nodejs.org/)
29+
::: zone-end
30+
31+
32+
## Get the sample code
33+
34+
::: zone pivot="programming-language-typescript"
35+
Find the sample code: [Socket.IO Serverless Sample (TS)](https://github.com/Azure/azure-webpubsub/tree/main/sdk/webpubsub-socketio-extension/examples/chat-serverless-typescript)
36+
37+
```bash
38+
git clone https://github.com/Azure/azure-webpubsub.git
39+
cd ./sdk/webpubsub-socketio-extension/examples/chat-serverless-typescript
40+
```
41+
::: zone-end
42+
43+
## Deploy infrastructure
44+
45+
The chat samples need to deploy several services in Azure:
46+
47+
- [Azure Function App](../azure-functions/functions-overview.md)
48+
- [Web PubSub for Socket.IO](./socketio-overview.md)
49+
- [Managed Identity](/entra/identity/managed-identities-azure-resources/how-manage-user-assigned-managed-identities): Identity for communicating between services
50+
51+
We use [Bicep](../azure-resource-manager/bicep/overview.md) to deploy the infrastructure. The file locates in the `./infra` folder. Deploy it with the az command:
52+
53+
```azcli
54+
az deployment sub create -n "<deployment-name>" -l "<deployment-location>" --template-file ./infra/main.bicep --parameters environmentName="<env-name>" location="<location>"
55+
```
56+
57+
- `<deployment-name>`: The name of the deployment.
58+
- `<deployment-location>`: The location of the deployment metadata. Note it's not the location where resources deploy to.
59+
- `<env-name>`: The name will be a part of the resource group name and resource name.
60+
- `<location>`: The location of the resources.
61+
62+
### Review of the infrastructure
63+
64+
In the infrastructure release, we deploy an Azure Function App in consumption plan as well as the Monitor and Storage Account that required by the Function App. We also deploy a Web PubSub for Socket.IO resource in Serverless Mode.
65+
66+
For the identity based authentication purpose, we deploye a user-assigned managed identity, assign it to both Function App and Socket.IO resource and grant it with some permissions:
67+
68+
- **Storage Blob Data Owner role**: Access storage for Function App
69+
- **Monitoring Metrics Publisher role**: Access monitor for Function App
70+
- **Web PubSub Service Owner role**: Access Web PubSub for Socket.IO for Function App
71+
72+
As per [Configure your Azure Functions app to use Microsoft Entra sign-in](../app-service/configure-authentication-provider-aad.md), we create a Service Principal. To avoid using secret for the Service Principal, we use [Federated identity credentials](/graph/api/resources/federatedidentitycredentials-overview).
73+
74+
## Deploy sample to the Function App
75+
76+
We prepared a bash script to deploy the sample code to the Function App:
77+
78+
```bash
79+
# Deploy the project
80+
./deploy/deploy.sh "<deployment-name>"
81+
```
82+
83+
### Review the deployment detail
84+
85+
We need to do two steps to deploy the sample app.
86+
87+
- Publish code to the Function App (Use Azure Functions Core Tools)
88+
89+
```bash
90+
func extensions sync
91+
npm install
92+
npm run build
93+
func azure functionapp publish <function-app-name>
94+
```
95+
96+
- Configure the Web PubSub for Socket.IO to add a hub setting which can send request to the Function App. As per the limitation of Function App's Webhook provider, you need to get an extension key populated by Function. Get more details in [Trigger Binding](./socketio-serverless-function-binding.md#trigger-binding). And as we use identity-based authentication, in the hub settings, you need to assign the target resource, which is the clientId of the Service Principal created before.
97+
98+
```bash
99+
code=$(az functionapp keys list -g <resource-group> -n <function-name> --query systemKeys.socketio_extension -o tsv)
100+
az webpubsub hub create -n <socketio-name> -g <resource-group> --hub-name "hub" --event-handler url-template="https://${<function-name>}.azurewebsites.net/runtime/webhooks/socketio?code=${code}" user-event-pattern="*" auth-type="ManagedIdentity" auth-resource="<service-principal-client-id>"
101+
```
102+
103+
### Run Sample App
104+
105+
After the code is deployed, visit the website to try the sample:
106+
107+
```bash
108+
https://<function-endpoint>/api/index
109+
```
110+
111+
:::image type="content" source="./media/socketio-serverless-quickstart/chatsample.png" alt-text="Screenshot of the serverless chat app.":::
112+
113+
## Next steps
114+
Next, you can follow the tutorial to write the app step by step:
115+
116+
> [!div class="nextstepaction"]
117+
> [Tutorial: Build chat app with Azure Function in Serverless Mode](./socketio-serverless-tutorial.md)

articles/azure-web-pubsub/socketio-serverless-tutorial.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,10 @@ func start
335335

336336
And visit the webpage at `http://localhost:7071/api/index`.
337337

338-
:::image type="content" source="./media/socketio-serverless-quickstart/chatsample.png" alt-text="Screenshot of the serverless chat app.":::
338+
:::image type="content" source="./media/socketio-serverless-tutorial/chatsample.png" alt-text="Screenshot of the serverless chat app.":::
339339

340340
## Next steps
341-
Next, you can explore more samples in various languages:
341+
Next, you can try to leverage Bicep to deploy the app online with identity-based authentication:
342342

343343
> [!div class="nextstepaction"]
344-
> [TypeScript Sample](https://github.com/Azure/azure-webpubsub/tree/main/sdk/webpubsub-socketio-extension/examples/chat-serverless-typescript)
345-
> [C# Sample](https://github.com/Azure/azure-webpubsub/tree/main/sdk/webpubsub-socketio-extension/examples/chat-serverless-dotnet)
344+
> [Quickstart: Build chat app with Azure Function in Socket.IO Serverless Mode](./socketio-serverless-quickstart.md)

articles/azure-web-pubsub/toc.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@
143143
href: socketio-overview.md
144144
- name: Serverless Mode Overview
145145
href: socketio-serverless-overview.md
146-
- name: Quickstart for Socket.IO users
147-
href: socketio-quickstart.md
146+
- name: Quickstart
147+
expanded: true
148+
items:
149+
- name: Quickstart for Socket.IO users
150+
href: socketio-quickstart.md
151+
- name: Quickstart for Socket.IO Serverless with Azure Function
152+
href: socketio-serverless-quickstart.md
148153
- name: Tutorials
149154
expanded: true
150155
items:

0 commit comments

Comments
 (0)