Skip to content

Commit 7afdff1

Browse files
authored
update
1 parent e7a10b5 commit 7afdff1

File tree

1 file changed

+98
-56
lines changed

1 file changed

+98
-56
lines changed

articles/azure-signalr/signalr-tutorial-authenticate-azure-functions.md

Lines changed: 98 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: In this tutorial, you learn how to authenticate Azure SignalR Servi
44
author: Y-Sindo
55
ms.service: signalr
66
ms.topic: tutorial
7-
ms.date: 02/16/2023
7+
ms.date: 03/18/2024
88
ms.author: zityang
99
ms.devlang: javascript
1010
ms.custom:
@@ -22,7 +22,7 @@ In this step-by-step tutorial, you build a chat room with authentication and pri
2222
## Prerequisites
2323

2424
- An Azure account with an active subscription. If you don't have one, you can [create one for free](https://azure.microsoft.com/free/).
25-
- [Node.js](https://nodejs.org/en/download/) (version 18.x).
25+
- [Node.js](https://nodejs.org/en/download/) (version 20.x).
2626
- [Azure Functions Core Tools](../azure-functions/functions-run-local.md?#install-the-azure-functions-core-tools) (version 4).
2727

2828
[Having issues? Let us know.](https://aka.ms/asrs/qsauth)
@@ -73,11 +73,20 @@ Your application will access an Azure SignalR Service instance. Use the followin
7373
### Initialize a function app
7474

7575
1. From a command line, create a root folder for your project and change to the folder.
76+
7677
1. Run the following command in your terminal to create a new JavaScript Functions project:
7778

78-
```bash
79-
func init --worker-runtime node --language javascript --name my-app
80-
```
79+
# [Model v4](#tab/nodejs-v4)
80+
81+
```bash
82+
func init --worker-runtime node --language javascript --name my-app --model V4
83+
```
84+
85+
# [Model v3](#tab/nodejs-v3)
86+
87+
```bash
88+
func init --worker-runtime node --language javascript --name my-app --model V3
89+
```
8190

8291
By default, the generated project includes a _host.json_ file that contains the extension bundles that include the SignalR extension. For more information about extension bundles, see [Register Azure Functions binding extensions](../azure-functions/functions-bindings-register.md#extension-bundles).
8392

@@ -117,57 +126,90 @@ When the chat app first opens in the browser, it requires valid connection crede
117126
> [!NOTE]
118127
> This function must be named `negotiate` because the SignalR client requires an endpoint that ends in `/negotiate`.
119128
120-
1. From the root project folder, create the `negotiate` function from a built-in template by using the following command:
121-
122-
```bash
123-
func new --template "SignalR negotiate HTTP trigger" --name negotiate
124-
```
125-
126-
1. Open _negotiate/function.json_ to view the function binding configuration.
127-
128-
The function contains an HTTP trigger binding to receive requests from SignalR clients. The function also contains a SignalR input binding to generate valid credentials for a client to connect to an Azure SignalR Service hub named `default`.
129-
130-
```json
131-
{
132-
"disabled": false,
133-
"bindings": [
134-
{
135-
"authLevel": "anonymous",
136-
"type": "httpTrigger",
137-
"direction": "in",
138-
"methods": ["post"],
139-
"name": "req",
140-
"route": "negotiate"
141-
},
142-
{
143-
"type": "http",
144-
"direction": "out",
145-
"name": "res"
146-
},
147-
{
148-
"type": "signalRConnectionInfo",
149-
"name": "connectionInfo",
150-
"hubName": "default",
151-
"connectionStringSetting": "AzureSignalRConnectionString",
152-
"direction": "in"
153-
}
154-
]
155-
}
156-
```
157-
158-
There's no `userId` property in the `signalRConnectionInfo` binding for local development. You'll add it later to set the username of a SignalR connection when you deploy the function app to Azure.
159-
160-
1. Close the _negotiate/function.json_ file.
161-
162-
1. Open _negotiate/index.js_ to view the body of the function:
163-
164-
```javascript
165-
module.exports = async function (context, req, connectionInfo) {
166-
context.res.body = connectionInfo;
167-
};
168-
```
169-
170-
This function takes the SignalR connection information from the input binding and returns it to the client in the HTTP response body. The SignalR client uses this information to connect to the Azure SignalR Service instance.
129+
# [Model v4](#tab/nodejs-v4)
130+
131+
1. From the root project folder, create the `negotiate` function from a built-in template by using the following command:
132+
133+
```bash
134+
func new --template "HTTP trigger" --name negotiate
135+
```
136+
137+
1. Open _src/functions/negotiate.js_, update the content as follows:
138+
139+
```javascript
140+
const { app, input } = require('@azure/functions');
141+
142+
const inputSignalR = input.generic({
143+
type: 'signalRConnectionInfo',
144+
name: 'connectionInfo',
145+
hubName: 'default',
146+
connectionStringSetting: 'AzureSignalRConnectionString',
147+
userId: '{query.x-ms-signalr-userid}'
148+
});
149+
150+
app.post('negotiate', {
151+
authLevel: 'function',
152+
handler: (request, context) => {
153+
return { body: JSON.stringify(context.extraInputs.get(inputSignalR)) }
154+
},
155+
route: 'negotiate',
156+
extraInputs: [inputSignalR],
157+
});
158+
```
159+
160+
The function contains an HTTP trigger binding to receive requests from SignalR clients. The function also contains a SignalR input binding to generate valid credentials for a client to connect to an Azure SignalR Service hub named `default`.
161+
162+
This function takes the SignalR connection information from the input binding and returns it to the client in the HTTP response body..
163+
164+
There's no `userId` property in the `signalRConnectionInfo` binding for local development. You'll add it later to set the username of a SignalR connection when you deploy the function app to Azure.
165+
166+
# [Model v3](#tab/nodejs-v3)
167+
168+
1. Open _negotiate/function.json_ to view the function binding configuration.
169+
170+
The function contains an HTTP trigger binding to receive requests from SignalR clients. The function also contains a SignalR input binding to generate valid credentials for a client to connect to an Azure SignalR Service hub named `default`.
171+
172+
```json
173+
{
174+
"disabled": false,
175+
"bindings": [
176+
{
177+
"authLevel": "anonymous",
178+
"type": "httpTrigger",
179+
"direction": "in",
180+
"methods": ["post"],
181+
"name": "req",
182+
"route": "negotiate"
183+
},
184+
{
185+
"type": "http",
186+
"direction": "out",
187+
"name": "res"
188+
},
189+
{
190+
"type": "signalRConnectionInfo",
191+
"name": "connectionInfo",
192+
"hubName": "default",
193+
"connectionStringSetting": "AzureSignalRConnectionString",
194+
"direction": "in"
195+
}
196+
]
197+
}
198+
```
199+
200+
There's no `userId` property in the `signalRConnectionInfo` binding for local development. You'll add it later to set the username of a SignalR connection when you deploy the function app to Azure.
201+
202+
1. Close the _negotiate/function.json_ file.
203+
204+
1. Open _negotiate/index.js_ to view the body of the function:
205+
206+
```javascript
207+
module.exports = async function (context, req, connectionInfo) {
208+
context.res.body = connectionInfo;
209+
};
210+
```
211+
212+
This function takes the SignalR connection information from the input binding and returns it to the client in the HTTP response body. The SignalR client uses this information to connect to the Azure SignalR Service instance.
171213

172214
[Having issues? Let us know.](https://aka.ms/asrs/qsauth)
173215

0 commit comments

Comments
 (0)