Skip to content

Commit 50115cf

Browse files
Merge pull request #260809 from JialinXin/patch-2
Add notes about input binding roles
2 parents 67fc8f0 + 410bcb2 commit 50115cf

1 file changed

Lines changed: 66 additions & 8 deletions

File tree

articles/azure-web-pubsub/reference-functions-bindings.md

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static void Run(
8080
}
8181
```
8282

83-
`WebPubSubTrigger` binding also supports return value in synchronize scenarios, for example, system `Connect` and user event, when server can check and deny the client request, or send messages to the caller directly. `Connect` event respects `ConnectEventResponse` and `EventErrorResponse`, and user event respects `UserEventResponse` and `EventErrorResponse`, rest types not matching current scenario will be ignored. And if `EventErrorResponse` is returned, service will drop the client connection.
83+
`WebPubSubTrigger` binding also supports return value in synchronize scenarios, for example, system `Connect` and user event, when server can check and deny the client request, or send messages to the caller directly. `Connect` event respects `ConnectEventResponse` and `EventErrorResponse`, and user event respects `UserEventResponse` and `EventErrorResponse`, rest types not matching current scenario is ignored. And if `EventErrorResponse` is returned, service drops the client connection.
8484

8585
```cs
8686
[FunctionName("WebPubSubTriggerReturnValueFunction")]
@@ -121,7 +121,7 @@ module.exports = function (context, data) {
121121
}
122122
```
123123

124-
`WebPubSubTrigger` binding also supports return value in synchronize scenarios, for example, system `Connect` and user event, when server can check and deny the client request, or send message to the request client directly. In JavaScript weakly typed language, it will be deserialized regarding the object keys. And `EventErrorResponse` will have the highest priority compare to rest objects, that if `code` is in the return, then it will be parsed to `EventErrorResponse` and client connection will be dropped.
124+
`WebPubSubTrigger` binding also supports return value in synchronize scenarios, for example, system `Connect` and user event, when server can check and deny the client request, or send message to the request client directly. In JavaScript weakly typed language, it's deserialized regarding the object keys. And `EventErrorResponse` has the highest priority compare to rest objects, that if `code` is in the return, then it's parsed to `EventErrorResponse` and client connection is dropped.
125125

126126
```js
127127
module.exports = async function (context) {
@@ -164,20 +164,20 @@ The following table explains the binding configuration properties that you set i
164164
| **hub** | Hub | Required - the value must be set to the name of the Web PubSub hub for the function to be triggered. We support set the value in attribute as higher priority, or it can be set in app settings as a global value. |
165165
| **eventType** | WebPubSubEventType | Required - the value must be set as the event type of messages for the function to be triggered. The value should be either `user` or `system`. |
166166
| **eventName** | EventName | Required - the value must be set as the event of messages for the function to be triggered. </br> For `system` event type, the event name should be in `connect`, `connected`, `disconnected`. </br> For user-defined subprotocols, the event name is `message`. </br> For system supported subprotocol `json.webpubsub.azure.v1.`, the event name is user-defined event name. |
167-
| **connection** | Connection | Optional - the name of an app settings or setting collection that specifies the upstream Azure Web PubSub service. The value will be used for signature validation. And the value will be auto resolved with app settings "WebPubSubConnectionString" by default. And `null` means the validation is not needed and will always succeed. |
167+
| **connection** | Connection | Optional - the name of an app settings or setting collection that specifies the upstream Azure Web PubSub service. The value is used for signature validation. And the value is auto resolved with app settings "WebPubSubConnectionString" by default. And `null` means the validation is not needed and always succeed. |
168168

169169
### Usages
170170

171171
In C#, `WebPubSubEventRequest` is type recognized binding parameter, rest parameters are bound by parameter name. Check table below of available parameters and types.
172172

173-
In weakly typed language like JavaScript, `name` in `function.json` will be used to bind the trigger object regarding below mapping table. And will respect `dataType` in `function.json` to convert message accordingly when `name` is set to `data` as the binding object for trigger input. All the parameters can be read from `context.bindingData.<BindingName>` and will be `JObject` converted.
173+
In weakly typed language like JavaScript, `name` in `function.json` is used to bind the trigger object regarding below mapping table. And respect `dataType` in `function.json` to convert message accordingly when `name` is set to `data` as the binding object for trigger input. All the parameters can be read from `context.bindingData.<BindingName>` and is `JObject` converted.
174174

175175
| Binding Name | Binding Type | Description | Properties |
176176
|---------|---------|---------|---------|
177177
|request|`WebPubSubEventRequest`|Describes the upstream request|Property differs by different event types, including derived classes `ConnectEventRequest`, `ConnectedEventRequest`, `UserEventRequest` and `DisconnectedEventRequest` |
178178
|connectionContext|`WebPubSubConnectionContext`|Common request information| EventType, EventName, Hub, ConnectionId, UserId, Headers, Origin, Signature, States |
179179
|data|`BinaryData`,`string`,`Stream`,`byte[]`| Request message data from client in user `message` event | -|
180-
|dataType|`WebPubSubDataType`| Request message dataType, supports `binary`, `text`, `json` | -|
180+
|dataType|`WebPubSubDataType`| Request message dataType, which supports `binary`, `text`, `json` | -|
181181
|claims|`IDictionary<string, string[]>`|User Claims in system `connect` request | -|
182182
|query|`IDictionary<string, string[]>`|User query in system `connect` request | -|
183183
|subprotocols|`IList<string>`|Available subprotocols in system `connect` request | -|
@@ -189,7 +189,7 @@ In weakly typed language like JavaScript, `name` in `function.json` will be used
189189
190190
### Return response
191191

192-
`WebPubSubTrigger` will respect customer returned response for synchronous events of `connect` and user event. Only matched response will be sent back to service, otherwise, it will be ignored. Besides, `WebPubSubTrigger` return object supports users to `SetState()` and `ClearStates()` to manage the metadata for the connection. And the extension will merge the results from return value with the original ones from request `WebPubSubConnectionContext.States`. Value in existing key will be overwrite and value in new key will be added.
192+
`WebPubSubTrigger` respects customer returned response for synchronous events of `connect` and user event. Only matched response is sent back to service, otherwise, it's ignored. Besides, `WebPubSubTrigger` return object supports users to `SetState()` and `ClearStates()` to manage the metadata for the connection. And the extension merges the results from return value with the original ones from request `WebPubSubConnectionContext.States`. Value in existing key is overwrite and value in new key is added.
193193

194194
| Return Type | Description | Properties |
195195
|---------|---------|---------|
@@ -286,6 +286,64 @@ public static WebPubSubConnection Run(
286286
}
287287
```
288288

289+
# [C#](#tab/csharp)
290+
291+
> [!NOTE]
292+
> Limited to the binding parameter types don't support a way to pass list nor array, the `WebPubSubConnection` is not fully supported with all the parameters server SDK has, especially `roles`, and also includes `groups` and `expiresAfter`. In the case customer needs to add roles or delay build the access token in the function, it's suggested to work with [server SDK for C#](/dotnet/api/overview/azure/messaging.webpubsub-readme?view=azure-dotnet).
293+
> ```cs
294+
> [FunctionName("WebPubSubConnectionCustomRoles")]
295+
> public static async Task<Uri> Run(
296+
> [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
297+
> {
298+
> var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "<hub>", "<web-pubsub-connection-string>");
299+
> var userId = req.Query["userid"].FirstOrDefault();
300+
> // your method to get custom roles.
301+
> var roles = GetRoles(userId);
302+
> return await serviceClient.GetClientAccessUriAsync(TimeSpan.FromMinutes(5), userId, roles);
303+
> }
304+
> ```
305+
306+
# [JavaScript](#tab/javascript)
307+
308+
> [!NOTE]
309+
> Limited to the binding parameter types don't support a way to pass list nor array, the `WebPubSubConnection` is not fully supported with all the parameters server SDK has, especially `roles`, and also includes `groups` and `expiresAfter`. In the case customer needs to add roles or delay build the access token in the function, it's suggested to work with [server SDK for JavaScript](/javascript/api/overview/azure/web-pubsub?view=azure-node-latest).
310+
>
311+
> Define input bindings in `function.json`.
312+
>
313+
> ```json
314+
> {
315+
> "disabled": false,
316+
> "bindings": [
317+
> {
318+
> "authLevel": "anonymous",
319+
> "type": "httpTrigger",
320+
> "direction": "in",
321+
> "name": "req"
322+
> },
323+
> {
324+
> "type": "http",
325+
> "direction": "out",
326+
> "name": "res"
327+
> }
328+
> ]
329+
> }
330+
> ```
331+
>
332+
> Define function in `index.js`.
333+
>
334+
> ```js
335+
> const { WebPubSubServiceClient } = require('@azure/web-pubsub');
336+
>
337+
> module.exports = async function (context, req) {
338+
> const serviceClient = new WebPubSubServiceClient(process.env.WebPubSubConnectionString, "<hub>");
339+
> token = await serviceClient.getAuthenticationToken({ userId: req.query.userid, roles: ["webpubsub.joinLeaveGroup", "webpubsub.sendToGroup"] });
340+
> context.res = { body: token.url };
341+
> context.done();
342+
> };
343+
> ```
344+
345+
---
346+
289347
### Example - `WebPubSubContext`
290348
291349
The following example shows a C# function that acquires Web PubSub upstream request information using the input binding under `connect` event type and returns it over HTTP.
@@ -381,7 +439,7 @@ The following table explains the binding configuration properties that you set i
381439
| **type** | n/a | Must be set to `webPubSubContext`. |
382440
| **direction** | n/a | Must be set to `in`. |
383441
| **name** | n/a | Variable name used in function code for input Web PubSub request. |
384-
| **connection** | Connection | Optional - the name of an app settings or setting collection that specifies the upstream Azure Web PubSub service. The value will be used for [Abuse Protection](https://github.com/cloudevents/spec/blob/v1.0.1/http-webhook.md#4-abuse-protection) and Signature validation. The value will be auto resolved with "WebPubSubConnectionString" by default. And `null` means the validation is not needed and will always succeed. |
442+
| **connection** | Connection | Optional - the name of an app settings or setting collection that specifies the upstream Azure Web PubSub service. The value is used for [Abuse Protection](https://github.com/cloudevents/spec/blob/v1.0.1/http-webhook.md#4-abuse-protection) and Signature validation. The value is auto resolved with "WebPubSubConnectionString" by default. And `null` means the validation is not needed and always succeed. |
385443

386444
### Usage
387445

@@ -432,7 +490,7 @@ For `WebPubSubEventRequest`, it's deserialized to different classes that provide
432490
| `DisconnectedEventRequest` | Used in system `Disconnected` event type | Reason |
433491

434492
> [!NOTE]
435-
> Though the `WebPubSubContext` is a input binding provides similar request deserialize way under `HttpTrigger` comparing to `WebPubSubTrigger`, there's limitations, i.e. connection state post merge is not supported. The return response will still be respected by the service side, but users require to build the response themselves. If users have needs to set the event response, you should return a `HttpResponseMessage` contains `ConnectEventResponse` or messages for user event as **response body** and put connection state with key `ce-connectionstate` in **response header**.
493+
> Though the `WebPubSubContext` is a input binding provides similar request deserialize way under `HttpTrigger` comparing to `WebPubSubTrigger`, there's limitations, i.e. connection state post merge is not supported. The return response is still respected by the service side, but users require to build the response themselves. If users have needs to set the event response, you should return a `HttpResponseMessage` contains `ConnectEventResponse` or messages for user event as **response body** and put connection state with key `ce-connectionstate` in **response header**.
436494
437495
## Output binding
438496

0 commit comments

Comments
 (0)