You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-web-pubsub/socketio-serverless-protocol.md
+140-3Lines changed: 140 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,12 +41,20 @@ Explanations of the previous sample:
41
41
- The `<hub-name>` in `path` is a concept in Web PubSub for Socket.IO, which provides isolation between hubs.
42
42
- The `<access-token>` is a JWT used to authenticate with the service. See (How to generate access token)[] for details.
43
43
44
-
### Authentication
44
+
### Authentication flow
45
45
46
46
When a client attempts to connect to the service, the process is divided into two distinct steps: establishing an Engine.IO (physical) connection and connecting to a namespace, which is referred to as a socket in Socket.IO terminology. The authentication process differs between these two steps:
47
47
48
48
1.**Engine.IO connection**: During this step, the service authenticates the client using an access token to determine whether to accept the connection. If the corresponding hub is configured to allow anonymous mode, the Engine.IO connection can proceed without validating the access token. However, for security reasons, it's recommended to disable anonymous mode in production environments.
49
49
50
+
- The Engine.IO connection url follow the format as shown below. But in most cases, it should be handled by Socket.IO client library.
- The details of access token can be found in [here](#authentication-details)
57
+
50
58
2. **Socket**: After the Engine.IO connection is successfully established, the client SDK sends a payload to connect to a namespace. Upon receiving the socket connect request, the service triggers a connect call to the event handler. The outcome of this step depends on the status code returned by the connect response: a 200 status code indicates that the socket is approved, while a 4xx or 5xx status code results in the socket being rejected.
51
59
52
60
3. Once a socket is connected, the service triggers a connected call to the event handler. It's an asynchronized call to notify the event handler a socket is successfully connected.
@@ -75,6 +83,89 @@ The event handler may respond with a body like `{ type: ACK, namespace: "/", dat
75
83
76
84
Client disconnects from a namespace or the corresponding Engine.IO connection closes results in socket close. Service triggers a disconnected event for every disconnected socket. It's an asynchronized call for notification.
77
85
86
+
## Authentication Details
87
+
88
+
The service uses bearer token to authenticate. There're two main scenario to use the token.
89
+
90
+
- Connect of Engine.IO connection. The following request is an example.
The `AccessKey` can be get from the service Azure Portal or from the Azure Cli:
143
+
144
+
```azcli
145
+
az webpubsub key show -g <resource-group> -n <resource-name>
146
+
```
147
+
148
+
### **Identity based authentication**
149
+
150
+
#### Token for RESTful API
151
+
152
+
Identity based authentication uses an [`access token`](/entra/identity-platform/access-tokens) signed by Microsoft identity platform.
153
+
154
+
The application which is used to request a token must use the resource `https://webpubsub.azure.com` or scope `https://webpubsub.azure.com/.default`. And it needs to be granted `Web PubSub Service Owner` Role. For more detail, see [Authorize access to Web PubSub resources using Microsoft Entra ID](./concept-azure-ad-authorization.md)
155
+
156
+
#### Token for Engine.IO connection
157
+
158
+
Different from the RESTful API, Engine.IO connection doesn't use the Entra ID token directly. Instead, you must make a RESTful call to the service to get a token and use the returned token as the access token for client.
159
+
160
+
```Http
161
+
POST {endpoint}/api/hubs/{hub}/:generateToken?api-version=2024-01-01
162
+
163
+
Headers:
164
+
Authorization: Bearer <Entra ID Token>
165
+
```
166
+
167
+
For more optional parameters, see [Generate Client Token](/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token)
168
+
78
169
## Supported functionality and RESTful APIs
79
170
80
171
A server can use RESTful APIs to manage Socket.IO clients and send message to clients as well. As Socket.IO reuses the Web PubSub service RESTful APIs, Socket.IO terminology is transformed into Web PubSub terminology. The following documents elaborate the transformation.
@@ -117,6 +208,10 @@ A Socket ID uniquely identifies a socket connection. According to the Socket.IO
117
208
118
209
```Http
119
210
POST {endpoint}/api/hubs/{hub}/:addToGroups?api-version=2024-01-01
211
+
212
+
Headers:
213
+
Authorization: Bearer <access token>
214
+
Content-Type: application/json
120
215
```
121
216
122
217
#### Request Body
@@ -136,8 +231,13 @@ Add socket `socketId` in namespace `/ns` to room `rm` in hub `myHub`.
136
231
```HTTP
137
232
POST {endpoint}/api/hubs/myHub/:addToGroups?api-version=2024-01-01
138
233
234
+
Headers:
235
+
Authorization: Bearer <access token>
236
+
Content-Type: application/json
237
+
238
+
Body:
139
239
{
140
-
"filter": "'0~L25z~c29ja2V0SWQ' in groups"
240
+
"filter": "'0~L25z~c29ja2V0SWQ' in groups",
141
241
"groups": [ "'0~L25z~cm0" ]
142
242
}
143
243
```
@@ -146,6 +246,10 @@ POST {endpoint}/api/hubs/myHub/:addToGroups?api-version=2024-01-01
146
246
147
247
```Http
148
248
POST {endpoint}/api/hubs/{hub}/:removeFromGroups?api-version=2024-01-01
249
+
250
+
Headers:
251
+
Authorization: Bearer <access token>
252
+
Content-Type: application/json
149
253
```
150
254
151
255
#### Request Body
@@ -165,8 +269,13 @@ Remove socket `socketId` in namespace `/ns` from room `rm` in hub `myHub`.
165
269
```HTTP
166
270
POST {endpoint}/api/hubs/myHub/:removeFromGroups?api-version=2024-01-01
167
271
272
+
Headers:
273
+
Authorization: Bearer <access token>
274
+
Content-Type: application/json
275
+
276
+
Body:
168
277
{
169
-
"filter": "'0~L25z~c29ja2V0SWQ' in groups"
278
+
"filter": "'0~L25z~c29ja2V0SWQ' in groups",
170
279
"groups": [ "'0~L25z~cm0" ]
171
280
}
172
281
```
@@ -176,6 +285,8 @@ POST {endpoint}/api/hubs/myHub/:removeFromGroups?api-version=2024-01-01
176
285
```Http
177
286
POST {endpoint}/api/hubs/{hub}/groups/{group}/:send?api-version=2024-01-01
0 commit comments