Skip to content

Commit 3a71aa8

Browse files
authored
Add python
1 parent b2f44be commit 3a71aa8

File tree

1 file changed

+227
-2
lines changed

1 file changed

+227
-2
lines changed

articles/azure-web-pubsub/socketio-serverless-function-binding.md

Lines changed: 227 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ app.http('negotiate', {
144144

145145
---
146146

147+
# [Python Model v2](#tab/python-v2)
148+
149+
A function always needs a trigger binding. We use HttpTrigger as an example in codes.
150+
151+
```python
152+
import azure.functions as func
153+
app = func.FunctionApp()
154+
155+
@app.function_name(name="negotiate")
156+
@app.route(auth_level=func.AuthLevel.ANONYMOUS)
157+
@app.generic_input_binding(arg_name="negotiate", type="socketionegotiation", hub="hub")
158+
def negotiate(req: func.HttpRequest, negotiate) -> func.HttpResponse:
159+
return func.HttpResponse(negotiate)
160+
```
161+
162+
### Annotation
163+
164+
| Property | Description |
165+
|---------|---------|
166+
| arg_name | The variable name of the argument in function to represent the input binding. |
167+
| type | Must be `socketionegotiation` |
168+
| hub | The hub name that a client needs to connect to. |
169+
| connection | The name of the app setting that contains the Socket.IO connection string (defaults to `WebPubSubForSocketIOConnectionString`). |
170+
| userId | The userId of the connection. It applys to all sockets in the connection. It becomes the `sub` claim in the generated token. |
171+
172+
---
173+
147174
## Trigger Binding
148175

149176
Azure Function uses trigger binding to trigger a function to process the events from the Web PubSub for Socket.IO.
@@ -213,8 +240,6 @@ The attribute for trigger binding is `[SocketIOTrigger]`.
213240

214241
`[SocketIOTrigger]` binds some variables to binding data. You can learn more about it from [Azure Functions binding expression patterns](../azure-functions/functions-bindings-expressions-patterns.md)
215242

216-
217-
218243
#### SocketIOAttribute
219244

220245
`SocketIOAttribute` is an alternative of `ParameterNames`, which simplify the function definition. For example, the following two definitions have the same effection:
@@ -331,6 +356,168 @@ app.generic('newMessage', {
331356

332357
---
333358

359+
# [Python Model v2](#tab/python-v2)
360+
361+
Function triggers for socket connect event.
362+
363+
```python
364+
import azure.functions as func
365+
import json
366+
app = func.FunctionApp()
367+
368+
@app.generic_trigger(arg_name="sio", type="socketiotrigger", data_type=DataType.STRING, hub="hub", eventName="connect")
369+
def connect(sio: str) -> str:
370+
return json.dumps({'statusCode': 200})
371+
```
372+
373+
Function triggers for socket connected event.
374+
375+
```python
376+
import azure.functions as func
377+
import json
378+
app = func.FunctionApp()
379+
380+
@app.generic_trigger(arg_name="sio", type="socketiotrigger", data_type=DataType.STRING, hub="hub", eventName="connected")
381+
def connected(sio: str) -> None:
382+
print("connected")
383+
```
384+
385+
Function triggers for socket disconnected event.
386+
387+
```python
388+
import azure.functions as func
389+
import json
390+
app = func.FunctionApp()
391+
392+
@app.generic_trigger(arg_name="sio", type="socketiotrigger", data_type=DataType.STRING, hub="hub", eventName="disconnected")
393+
def connected(sio: str) -> None:
394+
print("disconnected")
395+
```
396+
397+
Function triggers for normal messages from clients.
398+
399+
```python
400+
import azure.functions as func
401+
import json
402+
app = func.FunctionApp()
403+
404+
@app.generic_trigger(arg_name="sio", type="socketiotrigger", data_type=DataType.STRING, hub="hub", eventName="chat")
405+
def chat(sio: str) -> None:
406+
# do something else
407+
```
408+
409+
Function trigger for normal messages with callback.
410+
411+
```python
412+
import azure.functions as func
413+
import json
414+
app = func.FunctionApp()
415+
416+
@app.generic_trigger(arg_name="sio", type="socketiotrigger", data_type=DataType.STRING, hub="hub", eventName="chat")
417+
def chat(sio: str) -> str:
418+
return json.dumps({'ack': ["param1"]})
419+
```
420+
421+
### Configuration
422+
423+
| Property | Description |
424+
|---------|---------|
425+
| arg_name | The variable name of the argument in function to represent the trigger binding. |
426+
| type | Must be `socketiotrigger` |
427+
| hub | The hub name that a client needs to connect to. |
428+
| data_type | Must be `DataType.STRING. |
429+
| namespace | The namespace of the socket. Default: "/" |
430+
| eventName | The event name that the function triggers for. Some event names are predefined: `connect` for socket connect event. `connected` for socket connected event. `disconnected` for socket disconnected event. And other events are defined by user and it need to match the event name sent by client side. |
431+
432+
---
433+
434+
### Request of Input Binding
435+
436+
The data structure of input binding arguments varies depending on the message type.
437+
438+
#### Connect
439+
440+
```json
441+
{
442+
"namespace": "",
443+
"socketId": "",
444+
"claims": {
445+
"<claim-type>": [ "<claim-value>" ]
446+
},
447+
"query": {
448+
"<query-key>": [ "<query-value>" ]
449+
},
450+
"headers":{
451+
"<header-name>": [ "<header-value>" ]
452+
},
453+
"clientCertificates":{
454+
{
455+
"thumbprint": "",
456+
"content": ""
457+
}
458+
}
459+
}
460+
```
461+
462+
| Property | Description |
463+
|---------|---------|
464+
| namespace | The namespace of the soceket. |
465+
| socketId | The unique identity of the soceket. |
466+
| claims | The claim of jwt of the client connection. Note, it's not the jwt when the service request the function, but the jwt when the Engine.IO client connects to the service. |
467+
| query | The query of the client connection. Note, it's not the query when the service request the function, but the query when the Engine.IO client connects to the service. |
468+
| headers | The headers of the client connection. Note, it's not the headers when the service request the function, but the headers when the Engine.IO client connects to the service. |
469+
| clientCertificates | The client certificate if it's enabled |
470+
471+
#### Connected
472+
473+
```json
474+
{
475+
"namespace": "",
476+
"socketId": "",
477+
}
478+
```
479+
480+
| Property | Description |
481+
|---------|---------|
482+
| namespace | The namespace of the soceket. |
483+
| socketId | The unique identity of the soceket. |
484+
485+
#### Disconnected
486+
487+
```json
488+
{
489+
"namespace": "",
490+
"socketId": "",
491+
"reason": ""
492+
}
493+
```
494+
495+
| Property | Description |
496+
|---------|---------|
497+
| namespace | The namespace of the soceket. |
498+
| socketId | The unique identity of the soceket. |
499+
| reason | The connection close reason description. |
500+
501+
#### Normal events
502+
503+
```json
504+
{
505+
"namespace": "",
506+
"socketId": "",
507+
"payload": "",
508+
"eventName": "",
509+
"parameters": []
510+
}
511+
```
512+
513+
| Property | Description |
514+
|---------|---------|
515+
| namespace | The namespace of the soceket. |
516+
| socketId | The unique identity of the soceket. |
517+
| payload | The message payload in Engine.IO protocol |
518+
| eventName | The event name of the request. |
519+
| parameters | List of parameters of the message emittion. |
520+
334521
## Output Binding
335522

336523
The output binding currently support the following functionality:
@@ -406,6 +593,44 @@ app.generic('newMessage', {
406593

407594
---
408595

596+
# [Python Model v2](#tab/python-v2)
597+
598+
A function always needs a trigger binding. We use TimerTrigger as an example in codes.
599+
600+
```python
601+
import azure.functions as func
602+
from azure.functions.decorators.core import DataType
603+
import json
604+
605+
app = func.FunctionApp()
606+
607+
@app.timer_trigger(schedule="* * * * * *", arg_name="myTimer", run_on_startup=False,
608+
use_monitor=False)
609+
@app.generic_output_binding(arg_name="sio", type="socketio", data_type=DataType.STRING, hub="hub")
610+
def new_message(myTimer: func.TimerRequest,
611+
sio: func.Out[str]) -> None:
612+
sio.set(json.dumps({
613+
'actionName': 'sendToNamespace',
614+
'namespace': '/',
615+
'eventName': 'update',
616+
'parameters': [
617+
"message"
618+
]
619+
}))
620+
```
621+
622+
### Annotation
623+
624+
| Attribute property | Description |
625+
|---------|---------|
626+
| arg_name | The variable name of the argument in function to represent the output binding. |
627+
| type | Must be `socketio` |
628+
| data_type | Use `DataType.STRING` |
629+
| hub | The hub name that a client needs to connect to. |
630+
| connection | The name of the app setting that contains the Socket.IO connection string (defaults to `WebPubSubForSocketIOConnectionString`). |
631+
632+
---
633+
409634
### Actions
410635

411636
Output binding uses actions to perform operations. Currently, we support the following actions:

0 commit comments

Comments
 (0)