|
| 1 | +--- |
| 2 | +author: vicancy |
| 3 | +ms.author: lianwei |
| 4 | +ms.service: azure-web-pubsub |
| 5 | +ms.topic: include |
| 6 | +ms.date: 08/06/2021 |
| 7 | +--- |
| 8 | + |
| 9 | +### Join groups |
| 10 | + |
| 11 | +Format: |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "type": "joinGroup", |
| 16 | + "group": "<group_name>", |
| 17 | + "ackId" : 1 |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +* `ackId` is the identity of each request and should be unique. The service sends a [ack response message](#ack-response) to notify the process result of the request. More details can be found at [AckId and Ack Response](../concept-client-protocols.md#ackid-and-ack-response) |
| 22 | + |
| 23 | +### Leave groups |
| 24 | + |
| 25 | +Format: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "type": "leaveGroup", |
| 30 | + "group": "<group_name>", |
| 31 | + "ackId" : 1 |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +* `ackId` is the identity of each request and should be unique. The service sends a [ack response message](#ack-response) to notify the process result of the request. More details can be found at [AckId and Ack Response](../concept-client-protocols.md#ackid-and-ack-response) |
| 36 | + |
| 37 | +### Publish messages |
| 38 | + |
| 39 | +Format: |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "type": "sendToGroup", |
| 44 | + "group": "<group_name>", |
| 45 | + "ackId" : 1, |
| 46 | + "noEcho": true|false, |
| 47 | + "dataType" : "json|text|binary", |
| 48 | + "data": {}, // data can be string or valid json token depending on the dataType |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +* `ackId` is the identity of each request and should be unique. The service sends a [ack response message](#ack-response) to notify the process result of the request. More details can be found at [AckId and Ack Response](../concept-client-protocols.md#ackid-and-ack-response) |
| 53 | +* `noEcho` is optional. If set to true, this message is not echoed back to the same connection. If not set, the default value is false. |
| 54 | +* `dataType` can be one of `json`, `text`, or `binary`: |
| 55 | + * `json`: `data` can be any type that JSON supports and will be published as what it is; If `dataType` isn't specified, it defaults to `json`. |
| 56 | + * `text`: `data` should be in string format, and the string data will be published; |
| 57 | + * `binary`: `data` should be in base64 format, and the binary data will be published; |
| 58 | + |
| 59 | +#### Case 1: publish text data: |
| 60 | +```json |
| 61 | +{ |
| 62 | + "type": "sendToGroup", |
| 63 | + "group": "<group_name>", |
| 64 | + "dataType" : "text", |
| 65 | + "data": "text data", |
| 66 | + "ackId": 1 |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +* What subprotocol client in this group `<group_name>` receives: |
| 71 | +```json |
| 72 | +{ |
| 73 | + "type": "message", |
| 74 | + "from": "group", |
| 75 | + "group": "<group_name>", |
| 76 | + "dataType" : "text", |
| 77 | + "data" : "text data" |
| 78 | +} |
| 79 | +``` |
| 80 | +* What the raw client in this group `<group_name>` receives is string data `text data`. |
| 81 | + |
| 82 | +#### Case 2: publish JSON data: |
| 83 | +```json |
| 84 | +{ |
| 85 | + "type": "sendToGroup", |
| 86 | + "group": "<group_name>", |
| 87 | + "dataType" : "json", |
| 88 | + "data": { |
| 89 | + "hello": "world" |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +* What subprotocol client in this group `<group_name>` receives: |
| 95 | +```json |
| 96 | +{ |
| 97 | + "type": "message", |
| 98 | + "from": "group", |
| 99 | + "group": "<group_name>", |
| 100 | + "dataType" : "json", |
| 101 | + "data" : { |
| 102 | + "hello": "world" |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | +* What the raw client in this group `<group_name>` receives is serialized string data `{"hello": "world"}`. |
| 107 | + |
| 108 | + |
| 109 | +#### Case 3: publish binary data: |
| 110 | +```json |
| 111 | +{ |
| 112 | + "type": "sendToGroup", |
| 113 | + "group": "<group_name>", |
| 114 | + "dataType" : "binary", |
| 115 | + "data": "<base64_binary>", |
| 116 | + "ackId": 1 |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +* What subprotocol client in this group `<group_name>` receives: |
| 121 | +```json |
| 122 | +{ |
| 123 | + "type": "message", |
| 124 | + "from": "group", |
| 125 | + "group": "<group_name>", |
| 126 | + "dataType" : "binary", |
| 127 | + "data" : "<base64_binary>", |
| 128 | +} |
| 129 | +``` |
| 130 | +* What the raw client in this group `<group_name>` receives is the **binary** data in the binary frame. |
| 131 | + |
| 132 | +### Send custom events |
| 133 | + |
| 134 | +Format: |
| 135 | + |
| 136 | +```json |
| 137 | +{ |
| 138 | + "type": "event", |
| 139 | + "event": "<event_name>", |
| 140 | + "ackId": 1, |
| 141 | + "dataType" : "json|text|binary", |
| 142 | + "data": {}, // data can be string or valid json token depending on the dataType |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +* `ackId` is the identity of each request and should be unique. The service sends a [ack response message](#ack-response) to notify the process result of the request. More details can be found at [AckId and Ack Response](../concept-client-protocols.md#ackid-and-ack-response) |
| 147 | + |
| 148 | +`dataType` can be one of `text`, `binary`, or `json`: |
| 149 | +* `json`: data can be any type json supports and will be published as what it is; If `dataType` is not specified, it defaults to `json`. |
| 150 | +* `text`: data should be in string format, and the string data will be published; |
| 151 | +* `binary`: data should be in base64 format, and the binary data will be published; |
| 152 | + |
| 153 | +#### Case 1: send event with text data: |
| 154 | +```json |
| 155 | +{ |
| 156 | + "type": "event", |
| 157 | + "event": "<event_name>", |
| 158 | + "ackId": 1, |
| 159 | + "dataType" : "text", |
| 160 | + "data": "text data", |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +What the upstream event handler receives like below, the `Content-Type` for the CloudEvents HTTP request is `text/plain` for `dataType`=`text` |
| 165 | + |
| 166 | +```HTTP |
| 167 | +POST /upstream HTTP/1.1 |
| 168 | +Host: xxxxxx |
| 169 | +WebHook-Request-Origin: xxx.webpubsub.azure.com |
| 170 | +Content-Type: text/plain |
| 171 | +Content-Length: nnnn |
| 172 | +ce-specversion: 1.0 |
| 173 | +ce-type: azure.webpubsub.user.<event_name> |
| 174 | +ce-source: /client/{connectionId} |
| 175 | +ce-id: {eventId} |
| 176 | +ce-time: 2021-01-01T00:00:00Z |
| 177 | +ce-signature: sha256={connection-id-hash-primary},sha256={connection-id-hash-secondary} |
| 178 | +ce-userId: {userId} |
| 179 | +ce-connectionId: {connectionId} |
| 180 | +ce-hub: {hub_name} |
| 181 | +ce-eventName: <event_name> |
| 182 | +
|
| 183 | +text data |
| 184 | +
|
| 185 | +``` |
| 186 | + |
| 187 | +#### Case 2: send event with JSON data: |
| 188 | +```json |
| 189 | +{ |
| 190 | + "type": "event", |
| 191 | + "event": "<event_name>", |
| 192 | + "ackId": 1, |
| 193 | + "dataType" : "json", |
| 194 | + "data": { |
| 195 | + "hello": "world" |
| 196 | + }, |
| 197 | +} |
| 198 | +``` |
| 199 | + |
| 200 | +What the upstream event handler receives like below, the `Content-Type` for the CloudEvents HTTP request is `application/json` for `dataType`=`json` |
| 201 | + |
| 202 | +```HTTP |
| 203 | +POST /upstream HTTP/1.1 |
| 204 | +Host: xxxxxx |
| 205 | +WebHook-Request-Origin: xxx.webpubsub.azure.com |
| 206 | +Content-Type: application/json |
| 207 | +Content-Length: nnnn |
| 208 | +ce-specversion: 1.0 |
| 209 | +ce-type: azure.webpubsub.user.<event_name> |
| 210 | +ce-source: /client/{connectionId} |
| 211 | +ce-id: {eventId} |
| 212 | +ce-time: 2021-01-01T00:00:00Z |
| 213 | +ce-signature: sha256={connection-id-hash-primary},sha256={connection-id-hash-secondary} |
| 214 | +ce-userId: {userId} |
| 215 | +ce-connectionId: {connectionId} |
| 216 | +ce-hub: {hub_name} |
| 217 | +ce-eventName: <event_name> |
| 218 | +
|
| 219 | +{ |
| 220 | + "hello": "world" |
| 221 | +} |
| 222 | +
|
| 223 | +``` |
| 224 | + |
| 225 | +#### Case 3: send event with binary data: |
| 226 | +```json |
| 227 | +{ |
| 228 | + "type": "event", |
| 229 | + "event": "<event_name>", |
| 230 | + "ackId": 1, |
| 231 | + "dataType" : "binary", |
| 232 | + "data": "base64_binary", |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +What the upstream event handler receives like below, the `Content-Type` for the CloudEvents HTTP request is `application/octet-stream` for `dataType`=`binary` |
| 237 | + |
| 238 | +```HTTP |
| 239 | +POST /upstream HTTP/1.1 |
| 240 | +Host: xxxxxx |
| 241 | +WebHook-Request-Origin: xxx.webpubsub.azure.com |
| 242 | +Content-Type: application/octet-stream |
| 243 | +Content-Length: nnnn |
| 244 | +ce-specversion: 1.0 |
| 245 | +ce-type: azure.webpubsub.user.<event_name> |
| 246 | +ce-source: /client/{connectionId} |
| 247 | +ce-id: {eventId} |
| 248 | +ce-time: 2021-01-01T00:00:00Z |
| 249 | +ce-signature: sha256={connection-id-hash-primary},sha256={connection-id-hash-secondary} |
| 250 | +ce-userId: {userId} |
| 251 | +ce-connectionId: {connectionId} |
| 252 | +ce-hub: {hub_name} |
| 253 | +ce-eventName: <event_name> |
| 254 | +
|
| 255 | +binary |
| 256 | +
|
| 257 | +``` |
| 258 | + |
| 259 | +The WebSocket frame can be `text` format for text message frames or UTF8 encoded binaries for `binary` message frames. |
| 260 | + |
| 261 | +Service declines the client if the message does not match the described format. |
0 commit comments