Skip to content

Commit e34d4f1

Browse files
authored
Added v2 example for signalr
1 parent 377b9ea commit e34d4f1

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

articles/azure-signalr/signalr-quickstart-azure-functions-python.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ This quickstart can be run on macOS, Windows, or Linux. You will need the follo
3333

3434
[!INCLUDE [Create instance](includes/signalr-quickstart-create-instance.md)]
3535

36+
#### [Python v1 model](#tab/python-v1)
37+
3638
## Create the Azure Function project
3739

3840
Create a local Azure Function project.
@@ -43,7 +45,7 @@ Create a local Azure Function project.
4345

4446
```bash
4547
# Initialize a function project
46-
func init --worker-runtime python
48+
func init --worker-runtime python --model v1
4749
```
4850

4951
## Create the functions
@@ -213,6 +215,104 @@ You can use this sample function as a template for your own functions.
213215
}))
214216
```
215217

218+
#### [Python v2 model](#tab/python-v2)
219+
220+
## Create the Azure Function project
221+
222+
Create a local Azure Function project.
223+
224+
1. From a command line, create a directory for your project.
225+
1. Change to the project directory.
226+
1. Use the Azure Functions `func init` command to initialize your function project.
227+
228+
```bash
229+
# Initialize a function project
230+
func init --worker-runtime python
231+
```
232+
233+
## Create the functions
234+
235+
After you initialize a project, you need to create functions. This project requires three functions:
236+
237+
- `index`: Hosts a web page for a client.
238+
- `negotiate`: Allows a client to get an access token.
239+
- `broadcast`: Uses a time trigger to periodically broadcast messages to all clients.
240+
241+
When you run the `func new` command from the root directory of the project, the Azure Functions Core Tools appends the function code in the `function_app.py` file. You'll edit the parameters ad content as necessary by replacing the default code with the app code.
242+
243+
### Create the index function
244+
245+
You can use this sample function as a template for your own functions.
246+
247+
Open the file `function_app.py`. This file will contain your functions. First, modify the file to include the neccessary import statements, and define global variables that we will be using in the following functions.
248+
249+
```python
250+
import azure.functions as func
251+
import os
252+
import requests
253+
import json
254+
255+
app = func.FunctionApp()
256+
257+
etag = ''
258+
start_count = 0
259+
```
260+
261+
2. Add the function `index` by adding the following code
262+
263+
```python
264+
@app.route(route="index", auth_level=func.AuthLevel.ANONYMOUS)
265+
def index(req: func.HttpRequest) -> func.HttpResponse:
266+
f = open(os.path.dirname(os.path.realpath(__file__)) + '/content/index.html')
267+
return func.HttpResponse(f.read(), mimetype='text/html')
268+
```
269+
270+
This function hosts a web page for a client.
271+
272+
### Create the negotiate function
273+
274+
Add the function `negotiate` by adding the following code
275+
276+
```python
277+
@app.route(route="negotiate", auth_level=func.AuthLevel.ANONYMOUS, methods=["POST"])
278+
@app.generic_input_binding(arg_name="connectionInfo", type="signalRConnectionInfo", hubName="serverless", connectionStringSetting="AzureSignalRConnectionString")
279+
def negotiate(req: func.HttpRequest, connectionInfo) -> func.HttpResponse:
280+
return func.HttpResponse(connectionInfo)
281+
```
282+
283+
This function allows a client to get an access token.
284+
285+
### Create a broadcast function.
286+
287+
Add the function `broadcast` by adding the following code
288+
289+
```python
290+
@app.timer_trigger(schedule="*/1 * * * *", arg_name="myTimer",
291+
run_on_startup=False,
292+
use_monitor=False)
293+
@app.generic_output_binding(arg_name="signalRMessages", type="signalR", hubName="serverless", connectionStringSetting="AzureSignalRConnectionString")
294+
def broadcast(myTimer: func.TimerRequest, signalRMessages: func.Out[str]) -> None:
295+
global etag
296+
global start_count
297+
headers = {'User-Agent': 'serverless', 'If-None-Match': etag}
298+
res = requests.get('https://api.github.com/repos/azure/azure-functions-python-worker', headers=headers)
299+
if res.headers.get('ETag'):
300+
etag = res.headers.get('ETag')
301+
302+
if res.status_code == 200:
303+
jres = res.json()
304+
start_count = jres['stargazers_count']
305+
306+
signalRMessages.set(json.dumps({
307+
'target': 'newMessage',
308+
'arguments': [ 'Current star count of https://api.github.com/repos/azure/azure-functions-python-worker is: ' + str(start_count) ]
309+
}))
310+
```
311+
312+
This function uses a time trigger to periodically broadcast messages to all clients.
313+
314+
---
315+
216316
### Create the index.html file
217317

218318
The client interface for this app is a web page. The `index` function reads HTML content from the *content/index.html* file.

0 commit comments

Comments
 (0)