Skip to content

Commit 2d948c9

Browse files
author
Jill Grant
authored
Merge pull request #262862 from JialinXin/patch-5
[WebPubSub] Add Function Model v4 samples
2 parents 523f4a8 + 917faa7 commit 2d948c9

File tree

4 files changed

+611
-120
lines changed

4 files changed

+611
-120
lines changed

articles/azure-web-pubsub/quickstart-serverless.md

Lines changed: 143 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: JialinXin
55
ms.author: jixin
66
ms.service: azure-web-pubsub
77
ms.topic: tutorial
8-
ms.date: 01/02/2024
8+
ms.date: 01/12/2024
99
---
1010

1111
# Tutorial: Create a serverless real-time chat app with Azure Functions and Azure Web PubSub service
@@ -24,11 +24,22 @@ In this tutorial, you learn how to:
2424
2525
## Prerequisites
2626

27-
# [JavaScript](#tab/javascript)
27+
# [JavaScript Model v4](#tab/javascript-v4)
2828

2929
- A code editor, such as [Visual Studio Code](https://code.visualstudio.com/)
3030

31-
- [Node.js](https://nodejs.org/en/download/), version 10.x.
31+
- [Node.js](https://nodejs.org/en/download/), version 18.x or above.
32+
> [!NOTE]
33+
> For more information about the supported versions of Node.js, see [Azure Functions runtime versions documentation](../azure-functions/functions-versions.md#languages).
34+
- [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools#installing) (v4 or higher preferred) to run Azure Function apps locally and deploy to Azure.
35+
36+
- The [Azure CLI](/cli/azure) to manage Azure resources.
37+
38+
# [JavaScript Model v3](#tab/javascript-v3)
39+
40+
- A code editor, such as [Visual Studio Code](https://code.visualstudio.com/)
41+
42+
- [Node.js](https://nodejs.org/en/download/), version 18.x or above.
3243
> [!NOTE]
3344
> For more information about the supported versions of Node.js, see [Azure Functions runtime versions documentation](../azure-functions/functions-versions.md#languages).
3445
- [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools#installing) (v4 or higher preferred) to run Azure Function apps locally and deploy to Azure.
@@ -61,10 +72,16 @@ In this tutorial, you learn how to:
6172

6273
1. Make sure you have [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools#installing) installed. And then create an empty directory for the project. Run command under this working directory.
6374

64-
# [JavaScript](#tab/javascript)
75+
# [JavaScript Model v4](#tab/javascript-v4)
76+
77+
```bash
78+
func init --worker-runtime javascript --model V4
79+
```
80+
81+
# [JavaScript Model v3](#tab/javascript-v3)
6582

6683
```bash
67-
func init --worker-runtime javascript
84+
func init --worker-runtime javascript --model V3
6885
```
6986

7087
# [C# in-process](#tab/csharp-in-process)
@@ -81,13 +98,25 @@ In this tutorial, you learn how to:
8198

8299
2. Install `Microsoft.Azure.WebJobs.Extensions.WebPubSub`.
83100

84-
# [JavaScript](#tab/javascript)
101+
# [JavaScript Model v4](#tab/javascript-v4)
102+
103+
Confirm and update `host.json`'s extensionBundle to version _4.*_ or later to get Web PubSub support.
104+
105+
```json
106+
{
107+
"extensionBundle": {
108+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
109+
"version": "[4.*, 5.0.0)"
110+
}
111+
}
112+
```
113+
114+
# [JavaScript Model v3](#tab/javascript-v3)
85115

86-
Update `host.json`'s extensionBundle to version _3.3.0_ or later to get Web PubSub support.
116+
Confirm and update `host.json`'s extensionBundle to version _3.3.0_ or later to get Web PubSub support.
87117

88118
```json
89119
{
90-
"version": "2.0",
91120
"extensionBundle": {
92121
"id": "Microsoft.Azure.Functions.ExtensionBundle",
93122
"version": "[3.3.*, 4.0.0)"
@@ -112,8 +141,36 @@ In this tutorial, you learn how to:
112141
```bash
113142
func new -n index -t HttpTrigger
114143
```
144+
# [JavaScript Model v4](#tab/javascript-v4)
145+
146+
- Update `src/functions/index.js` and copy following codes.
147+
```js
148+
const { app } = require('@azure/functions');
149+
const { readFile } = require('fs/promises');
150+
151+
app.http('index', {
152+
methods: ['GET', 'POST'],
153+
authLevel: 'anonymous',
154+
handler: async (context) => {
155+
const content = await readFile('index.html', 'utf8', (err, data) => {
156+
if (err) {
157+
context.err(err)
158+
return
159+
}
160+
});
161+
162+
return {
163+
status: 200,
164+
headers: {
165+
'Content-Type': 'text/html'
166+
},
167+
body: content,
168+
};
169+
}
170+
});
171+
```
115172

116-
# [JavaScript](#tab/javascript)
173+
# [JavaScript Model v3](#tab/javascript-v3)
117174

118175
- Update `index/function.json` and copy following json codes.
119176
```json
@@ -205,7 +262,30 @@ In this tutorial, you learn how to:
205262
> [!NOTE]
206263
> In this sample, we use [Microsoft Entra ID](../app-service/configure-authentication-user-identities.md) user identity header `x-ms-client-principal-name` to retrieve `userId`. And this won't work in a local function. You can make it empty or change to other ways to get or generate `userId` when playing in local. For example, let client type a user name and pass it in query like `?user={$username}` when call `negotiate` function to get service connection url. And in the `negotiate` function, set `userId` with value `{query.user}`.
207264
208-
# [JavaScript](#tab/javascript)
265+
# [JavaScript Model v4](#tab/javascript-v4)
266+
- Update `src/functions/negotiate` and copy following codes.
267+
```js
268+
const { app, input } = require('@azure/functions');
269+
270+
const connection = input.generic({
271+
type: 'webPubSubConnection',
272+
name: 'connection',
273+
userId: '{headers.x-ms-client-principal-name}',
274+
hub: 'simplechat'
275+
});
276+
277+
app.http('negotiate', {
278+
methods: ['GET', 'POST'],
279+
authLevel: 'anonymous',
280+
extraInputs: [connection],
281+
handler: async (request, context) => {
282+
return { body: JSON.stringify(context.extraInputs.get('connection')) };
283+
},
284+
});
285+
```
286+
287+
288+
# [JavaScript Model v3](#tab/javascript-v3)
209289

210290
- Update `negotiate/function.json` and copy following json codes.
211291
```json
@@ -279,7 +359,45 @@ In this tutorial, you learn how to:
279359
func new -n message -t HttpTrigger
280360
```
281361
282-
# [JavaScript](#tab/javascript)
362+
# [JavaScript Model v4](#tab/javascript-v4)
363+
364+
- Update `src/functions/message.js` and copy following codes.
365+
```js
366+
const { app, output, trigger } = require('@azure/functions');
367+
368+
const wpsMsg = output.generic({
369+
type: 'webPubSub',
370+
name: 'actions',
371+
hub: 'simplechat',
372+
});
373+
374+
const wpsTrigger = trigger.generic({
375+
type: 'webPubSubTrigger',
376+
name: 'request',
377+
hub: 'simplechat',
378+
eventName: 'message',
379+
eventType: 'user'
380+
});
381+
382+
app.generic('message', {
383+
trigger: wpsTrigger,
384+
extraOutputs: [wpsMsg],
385+
handler: async (request, context) => {
386+
context.extraOutputs.set(wpsMsg, [{
387+
"actionName": "sendToAll",
388+
"data": `[${context.triggerMetadata.connectionContext.userId}] ${request.data}`,
389+
"dataType": request.dataType
390+
}]);
391+
392+
return {
393+
data: "[SYSTEM] ack.",
394+
dataType: "text",
395+
};
396+
}
397+
});
398+
```
399+
400+
# [JavaScript Model v3](#tab/javascript-v3)
283401
284402
- Update `message/function.json` and copy following json codes.
285403
```json
@@ -414,7 +532,9 @@ In this tutorial, you learn how to:
414532
</html>
415533
```
416534
417-
# [JavaScript](#tab/javascript)
535+
# [JavaScript Model v4](#tab/javascript-v4)
536+
537+
# [JavaScript Model v3](#tab/javascript-v3)
418538
419539
# [C# in-process](#tab/csharp-in-process)
420540
@@ -470,10 +590,19 @@ Use the following commands to create these items.
470590
471591
1. Create the function app in Azure:
472592
473-
# [JavaScript](#tab/javascript)
593+
# [JavaScript Model v4](#tab/javascript-v4)
594+
595+
```azurecli
596+
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime node --runtime-version 18 --functions-version 4 --name <FUNCIONAPP_NAME> --storage-account <STORAGE_NAME>
597+
```
598+
599+
> [!NOTE]
600+
> Check [Azure Functions runtime versions documentation](../azure-functions/functions-versions.md#languages) to set `--runtime-version` parameter to supported value.
601+
602+
# [JavaScript Model v3](#tab/javascript-v3)
474603
475604
```azurecli
476-
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime node --runtime-version 14 --functions-version 4 --name <FUNCIONAPP_NAME> --storage-account <STORAGE_NAME>
605+
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime node --runtime-version 18 --functions-version 4 --name <FUNCIONAPP_NAME> --storage-account <STORAGE_NAME>
477606
```
478607
479608
> [!NOTE]

0 commit comments

Comments
 (0)