Skip to content

Commit 4b456a8

Browse files
johanstevicancyiscai-msftscbedd
authored
Azure webpubsub service initial client library version (Azure#18199)
Added first version of webpubsub service client. Co-authored-by: Liangying.Wei <lianwei@microsoft.com> Co-authored-by: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Co-authored-by: scbedd <45376673+scbedd@users.noreply.github.com>
1 parent 71ec767 commit 4b456a8

File tree

23 files changed

+3212
-2
lines changed

23 files changed

+3212
-2
lines changed

eng/ignore-links.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ https://docs.microsoft.com/python/api/overview/azure/{{package_doc_id}}
22
https://github.com/Azure/azure-digital-twins/blob/private-preview/Documentation/how-to-manage-routes.md
33
https://pypi.org/project/azure-digitaltwins-core
44
https://docs.microsoft.com/azure/digital-twins/how-to-query-graph#query-limitations
5-
https://docs.microsoft.com/samples/azure-samples/azure-samples-python-management/{{package_doc_id}}
5+
https://docs.microsoft.com/samples/azure-samples/azure-samples-python-management/{{package_doc_id}}
6+
https://pypi.org/project/azure-messaging-webpubsubservice/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## 1.0.0b1
2+
3+
Initial version
4+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Microsoft
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include *.md
2+
include azure/__init__.py
3+
include azure/messaging/__init__.py
4+
include LICENSE.txt
5+
recursive-include tests *.py
6+
recursive-include examples *.py *.md
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Azure WebPubSubService client library for Python
2+
3+
[Azure Web PubSub Service](https://aka.ms/awps/doc) is a service that enables you to build real-time messaging web applications using WebSockets and the publish-subscribe pattern. Any platform supporting WebSocket APIs can connect to the service easily, e.g. web pages, mobile applications, edge devices, etc. The service manages the WebSocket connections for you and allows up to 100K \*concurrent connections. It provides powerful APIs for you to manage these clients and deliver real-time messages.
4+
5+
Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients, can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests, can also use Azure Web PubSub service.
6+
7+
We list some examples that are good to use Azure Web PubSub service:
8+
9+
- **High frequency data updates:** gaming, voting, polling, auction.
10+
- **Live dashboards and monitoring:** company dashboard, financial market data, instant sales update, multi-player game leader board, and IoT monitoring.
11+
- **Cross-platform live chat:** live chat room, chat bot, on-line customer support, real-time shopping assistant, messenger, in-game chat, and so on.
12+
- **Real-time location on map:** logistic tracking, delivery status tracking, transportation status updates, GPS apps.
13+
- **Real-time targeted ads:** personalized real-time push ads and offers, interactive ads.
14+
- **Collaborative apps:** coauthoring, whiteboard apps and team meeting software.
15+
- **Push instant notifications:** social network, email, game, travel alert.
16+
- **Real-time broadcasting:** live audio/video broadcasting, live captioning, translating, events/news broadcasting.
17+
- **IoT and connected devices:** real-time IoT metrics, remote control, real-time status, and location tracking.
18+
- **Automation:** real-time trigger from upstream events.
19+
20+
Use the client library to:
21+
22+
- Send messages to hubs and groups.
23+
- Send messages to particular users and connections.
24+
- Organize users and connections into groups.
25+
- Close connections
26+
- Grant/revoke/check permissions for an existing connection
27+
28+
[Source code](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/signalr/azure-messaging-webpubsubservice) | [Package (Pypi)][package] | [API reference documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/signalr/azure-messaging-webpubsubservice) | [Product documentation][webpubsubservice_docs]
29+
30+
## Getting started
31+
32+
### Installating the package
33+
34+
```bash
35+
python -m pip install azure-messaging-webpubsubservice
36+
```
37+
38+
#### Prequisites
39+
40+
- Python 2.7, or 3.6 or later is required to use this package.
41+
- You need an [Azure subscription][azure_sub], and a [Azure WebPubSub service instance][webpubsubservice_docs] to use this package.
42+
- An existing Azure Web PubSub service instance.
43+
44+
### Authenticating the client
45+
46+
In order to interact with the Azure WebPubSub service, you'll need to create an instance of the [WebPubSubServiceClient][webpubsubservice_client_class] class. In order to authenticate against the service, you need to pass in an AzureKeyCredential instance with endpoint and api key. The endpoint and api key can be found on the azure portal.
47+
48+
```python
49+
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
50+
>>> from azure.core.credentials import AzureKeyCredential
51+
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret'))
52+
>>> client
53+
<WebPubSubServiceClient endpoint:'<endpoint>'>
54+
```
55+
56+
## Examples
57+
58+
### Sending a request
59+
60+
```python
61+
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
62+
>>> from azure.core.credentials import AzureKeyCredential
63+
>>> from azure.messaging.webpubsubservice.rest import build_send_to_all_request
64+
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret'))
65+
>>> request = build_send_to_all_request('default', json={ 'Hello': 'webpubsub!' })
66+
>>> request
67+
<HttpRequest [POST], url: '/api/hubs/default/:send?api-version=2020-10-01'>
68+
>>> response = client.send_request(request)
69+
>>> response
70+
<RequestsTransportResponse: 202 Accepted>
71+
>>> response.status_code
72+
202
73+
>>> with open('file.json', 'r') as f:
74+
>>> request = build_send_to_all_request('ahub', content=f, content_type='application/json')
75+
>>> response = client.send_request(request)
76+
>>> print(response)
77+
<RequestsTransportResponse: 202 Accepted>
78+
```
79+
80+
## Key concepts
81+
82+
### Hub
83+
84+
Hub is a logical set of connections. All connections to Web PubSub connect to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. For example, hub can be used for different applications, different applications can share one Azure Web PubSub service by using different hub names.
85+
86+
### Group
87+
88+
Group allow broadcast messages to a subset of connections to the hub. You can add and remove users and connections as needed. A client can join multiple groups, and a group can contain multiple clients.
89+
90+
### User
91+
92+
Connections to Web PubSub can belong to one user. A user might have multiple connections, for example when a single user is connected across multiple devices or multiple browser tabs.
93+
94+
### Connection
95+
96+
Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique.
97+
98+
### Message
99+
100+
A message is either a UTF-8 encoded string, json or raw binary data.
101+
102+
## Troubleshooting
103+
104+
### Logging
105+
106+
This SDK uses Python standard logging library.
107+
You can configure logging print out debugging information to the stdout or anywhere you want.
108+
109+
```python
110+
import logging
111+
112+
logging.basicConfig(level=logging.DEBUG)
113+
````
114+
115+
Http request and response details are printed to stdout with this logging config.
116+
117+
## Next steps
118+
119+
More examples are coming soon...
120+
121+
## Contributing
122+
123+
This project welcomes contributions and suggestions. Most contributions require
124+
you to agree to a Contributor License Agreement (CLA) declaring that you have
125+
the right to, and actually do, grant us the rights to use your contribution.
126+
For details, visit https://cla.microsoft.com.
127+
128+
When you submit a pull request, a CLA-bot will automatically determine whether
129+
you need to provide a CLA and decorate the PR appropriately (e.g., label,
130+
comment). Simply follow the instructions provided by the bot. You will only
131+
need to do this once across all repos using our CLA.
132+
133+
This project has adopted the
134+
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
135+
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
136+
additional questions or comments.
137+
138+
<!-- LINKS -->
139+
[webpubsubservice_docs]: https://aka.ms/awps/doc
140+
[azure_cli]: https://docs.microsoft.com/cli/azure
141+
[azure_sub]: https://azure.microsoft.com/free/
142+
[webpubsubservice_client_class]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/signalr/azure-messaging-webpubsubservice/azure/messaging/webpubsubservice/__init__.py
143+
[package]: https://pypi.org/project/azure-messaging-webpubsubservice/
144+
[default_cred_ref]: https://aka.ms/azsdk-python-identity-default-cred-ref
145+
[cla]: https://cla.microsoft.com
146+
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
147+
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
148+
[coc_contact]: mailto:opencode@microsoft.com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

0 commit comments

Comments
 (0)