Skip to content

Commit 1dc4b90

Browse files
authored
Merge pull request #121521 from DamyanBG/#121474-webpubsub-quickstart-serverless-python-example
Add Python code example
2 parents 1c903d6 + 3da1be2 commit 1dc4b90

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

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

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ In this tutorial, you learn how to:
6363

6464
- The [Azure CLI](/cli/azure) to manage Azure resources.
6565

66+
# [Python](#tab/python)
67+
68+
- A code editor, such as [Visual Studio Code](https://code.visualstudio.com/).
69+
70+
- [Python](https://www.python.org/downloads/) (v3.7+). See [supported Python versions](../azure-functions/functions-reference-python.md#python-version).
71+
72+
- [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.
73+
74+
- The [Azure CLI](/cli/azure) to manage Azure resources.
75+
6676
---
6777

6878
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
@@ -97,6 +107,12 @@ In this tutorial, you learn how to:
97107
func init --worker-runtime dotnet-isolated
98108
```
99109

110+
# [Python](#tab/python)
111+
112+
```bash
113+
func init --worker-runtime python --model V1
114+
```
115+
100116
2. Install `Microsoft.Azure.WebJobs.Extensions.WebPubSub`.
101117

102118
# [JavaScript Model v4](#tab/javascript-v4)
@@ -137,6 +153,17 @@ In this tutorial, you learn how to:
137153
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.WebPubSub --prerelease
138154
```
139155

156+
# [Python](#tab/python)
157+
158+
```json
159+
{
160+
"extensionBundle": {
161+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
162+
"version": "[3.3.*, 4.0.0)"
163+
}
164+
```
165+
166+
140167
3. Create an `index` function to read and host a static web page for clients.
141168

142169
```bash
@@ -254,6 +281,45 @@ In this tutorial, you learn how to:
254281
}
255282
```
256283

284+
# [Python](#tab/python)
285+
286+
- Update `index/function.json` and copy following json codes.
287+
288+
```json
289+
{
290+
"bindings": [
291+
{
292+
"authLevel": "anonymous",
293+
"type": "httpTrigger",
294+
"direction": "in",
295+
"name": "req",
296+
"methods": ["get", "post"]
297+
},
298+
{
299+
"type": "http",
300+
"direction": "out",
301+
"name": "$return"
302+
}
303+
]
304+
}
305+
306+
```
307+
308+
309+
- Update `__init__.py` and replace `main` function with following codes.
310+
311+
```python
312+
import os
313+
314+
import azure.functions as func
315+
316+
317+
def main(req: func.HttpRequest) -> func.HttpResponse:
318+
f = open(os.path.dirname(os.path.realpath(__file__)) + "/../index.html")
319+
return func.HttpResponse(f.read(), mimetype="text/html")
320+
```
321+
322+
257323
4. Create a `negotiate` function to help clients get service connection url with access token.
258324

259325
```bash
@@ -354,6 +420,46 @@ In this tutorial, you learn how to:
354420
}
355421
```
356422
423+
# [Python](#tab/python)
424+
425+
- Update `negotiate/function.json` and copy following json codes.
426+
```json
427+
{
428+
"bindings": [
429+
{
430+
"authLevel": "anonymous",
431+
"type": "httpTrigger",
432+
"direction": "in",
433+
"name": "req"
434+
},
435+
{
436+
"type": "http",
437+
"direction": "out",
438+
"name": "$return"
439+
},
440+
{
441+
"type": "webPubSubConnection",
442+
"name": "connection",
443+
"hub": "simplechat",
444+
"userId": "{headers.x-ms-client-principal-name}",
445+
"direction": "in"
446+
}
447+
]
448+
}
449+
```
450+
451+
- Update `negotiate/__init__.py` and copy following codes.
452+
```python
453+
import azure.functions as func
454+
455+
456+
def main(req: func.HttpRequest, connection) -> func.HttpResponse:
457+
return func.HttpResponse(connection)
458+
459+
```
460+
461+
462+
357463
5. Create a `message` function to broadcast client messages through service.
358464
359465
```bash
@@ -483,6 +589,50 @@ In this tutorial, you learn how to:
483589
}
484590
```
485591
592+
# [Python](#tab/python)
593+
594+
- Update `message/function.json` and copy following json codes.
595+
```json
596+
{
597+
"bindings": [
598+
{
599+
"type": "webPubSubTrigger",
600+
"direction": "in",
601+
"name": "request",
602+
"hub": "simplechat",
603+
"eventName": "message",
604+
"eventType": "user"
605+
},
606+
{
607+
"type": "webPubSub",
608+
"name": "actions",
609+
"hub": "simplechat",
610+
"direction": "out"
611+
}
612+
]
613+
}
614+
```
615+
- Update `message/__init__.py` and copy following codes.
616+
```python
617+
import json
618+
619+
import azure.functions as func
620+
621+
622+
def main(request, actions: func.Out[str]) -> None:
623+
req_json = json.loads(request)
624+
actions.set(
625+
json.dumps(
626+
{
627+
"actionName": "sendToAll",
628+
"data": f'[{req_json["connectionContext"]["userId"]}] {req_json["data"]}',
629+
"dataType": req_json["dataType"],
630+
}
631+
)
632+
)
633+
```
634+
635+
486636
6. Add the client single page `index.html` in the project root folder and copy content.
487637
488638
```html
@@ -561,6 +711,8 @@ In this tutorial, you learn how to:
561711
</ItemGroup>
562712
```
563713
714+
# [Python](#tab/python)
715+
564716
## Create and Deploy the Azure Function App
565717
566718
Before you can deploy your function code to Azure, you need to create three resources:
@@ -621,6 +773,12 @@ Use the following commands to create these items.
621773
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime dotnet-isolated --functions-version 4 --name <FUNCIONAPP_NAME> --storage-account <STORAGE_NAME>
622774
```
623775
776+
# [Python](#tab/python)
777+
778+
```azurecli
779+
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime python --runtime-version 3.9 --functions-version 4 --name <FUNCIONAPP_NAME> --os-type linux --storage-account <STORAGE_NAME>
780+
```
781+
624782
1. Deploy the function project to Azure:
625783
626784
After you have successfully created your function app in Azure, you're now ready to deploy your local functions project by using the [func azure functionapp publish](./../azure-functions/functions-run-local.md) command.

0 commit comments

Comments
 (0)