Skip to content

Commit 499dd7f

Browse files
authored
Fix socketio sample
The sample provided was not working as it tried to use `polling`, which is unsupported by the notification URL you get back. I included the socket IO library and added a websocket as the transport protocol to make it work.
1 parent 0773e0e commit 499dd7f

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

api-reference/v1.0/api/subscriptions-socketio.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,30 @@ The `notificationUrl` returned is a socket.io endpoint URL.
113113

114114
The following example shows how to use the `notificationUrl` with socket.io in JavaScript.
115115

116-
```javascript
117-
// this is the notificationUrl returned from this API
118-
var notificationUrl = "https://f3hb0mpua.svc.ms/zbaehwg/callback?snthgk=1ff3-2345672zz831837523";
119-
120-
// 'io' comes from the socket.io client library
121-
var socket = io(notificationUrl);
122-
123-
// these examples log to the console.
124-
// your app would provide its own callbacks
125-
socket.on("connect", ()=>console.log("Connected!"));
126-
socket.on("notification", (data)=>console.log("Notification!", data));
116+
```html
117+
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.8.1/socket.io.js"></script>
118+
<script>
119+
// This is the notificationUrl returned from this API
120+
var notificationUrl = "https://f3hb0mpua.svc.ms/zbaehwg/callback?snthgk=1ff3-2345672zz831837523";
121+
122+
// 'io' comes from the socket.io client library
123+
var socket = io(notificationUrl, {
124+
transports: ['websocket'] // Make sure to use "websocket" as the default is set to "polling" which is not supported
125+
});
126+
127+
socket.on("connect", () => {
128+
console.log(`connect`, socket.id);
129+
});
130+
131+
socket.on("disconnect", () => {
132+
// Returns "undefined" on disconnect
133+
console.log(`disconnect`, socket.id);
134+
});
135+
136+
socket.on("notification", (data) => {
137+
console.log(`Notification received:`, data);
138+
});
139+
</script>
127140
```
128141

129142
<!-- uuid: 8fcb5dbc-d5aa-4681-8e31-b001d5168d79

0 commit comments

Comments
 (0)