Socket.IO client unable to connect to Microsoft Graph websocket endpoint

Mel Lim 0 Reputation points
2024-08-19T05:02:30.32+00:00

I followed the Microsoft Graph documentation at "Get websocket endpoint" (https://learn.microsoft.com/en-us/graph/api/subscriptions-socketio?view=graph-rest-1.0&tabs=javascript).

My get request to this endpoint: /sites/${siteId}/drive/root/subscriptions/socketIo (using the Javascript SDK) successfully provides a subscription object with a notification url.

Using the following, my socket instance does not connect to the MS Graph Socket IO server and instead encounters connection errors.

import { io } from "socket.io-client";


const socket = io(notificationUrl);
socket.on("connect", () => console.log("Connected!"));
socket.on("notification", (data) => console.log("Notification!", data));


socket.on("connect_error", (err) => console.error("Connect error", err))

Can anyone tell me what I am doing wrong?


Error logging callbacks attached to the "error" and "connect_error" events print the following two types of errors:

{
      "type": "TransportError",
      "message": "xhr post error",
      "stack": <OMITTED>,
      "description": 400,
      "context": {
        "UNSENT": 0,
        "OPENED": 1,
        "HEADERS_RECEIVED": 2,
        "LOADING": 3,
        "DONE": 4,
        "readyState": 4,
        "responseText": "{\"code\":1,\"message\":\"Session ID unknown\"}",
        "responseXML": "",
        "status": 400,
        "statusText": null
      }
    }




{
	"type": "TransportError",
	"message": "xhr poll error",
	"stack": <OMITTED>,
	"description": 400,
	"context": {
		"UNSENT": 0,
		"OPENED": 1,
		"HEADERS_RECEIVED": 2,
		"LOADING": 3,
		"DONE": 4,
		"readyState": 4,
		"responseText": "{\"code\":1,\"message\":\"Session ID unknown\"}",
		"responseXML": "",
		"status": 400,
		"statusText": null
	}
}
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,826 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.