-
Notifications
You must be signed in to change notification settings - Fork 192
Description
HI,
I am developing a new extension for Azure Devops and I would like to subscribe to some events generated by Azure Devops (e.g. a push of a new commit). I do not want to to use service hooks since this would require a server part, I would like just to leverage SignalR events which are used by Azure Devops.
I tried in two ways, but both failed. I search this repository for something relevant, yet I was unable to find any sample extension integrating with SignalR. What is the right way for an extension for connecting to the SignalR hub and subscribing an event ? I would be grateful if you could provide some insights or typescript/react code.
Thank you in advance.
Federico
- Trying to connect to SignalR hub using HubConnectionBuilder
As far as I understood the SignalR websockets server is located at this url:
/<organization name>/_apis/<project id>/signalr in my case https://dev.azure.com/fededim/_apis/cacb7072-0e17-4380-b28b-4afc9dea1c3a/signalr
I tried with the code below but the connection always fails with the error:
[2025-02-26T22:27:14.072Z] Error: Failed to start the connection: Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.
let _signalRConnection = new HubConnectionBuilder()
.withUrl('https://dev.azure.com/fededim/_apis/cacb7072-0e17-4380-b28b-4afc9dea1c3a/signalr',
{
accessTokenFactory: async () => await SDK.getAppToken(),
//headers: { ['Access-Control-Allow-Origin']: 'https://localhost:3000', ['Access-Control-Allow-Credentials']: 'true' },
withCredentials: false,
skipNegotiation: true,
transport: HttpTransportType.WebSockets
})
.configureLogging(LogLevel.Trace)
.build();
_signalRConnection.onclose((error) => {
console.error("SignalR closed with an error of: ", error);
debugger;
});
try {
console.debug('Connecting to SignalR...');
await _signalRConnection.start();
console.debug('Connected to SignalR!');
let ris = _signalRConnection.on('BranchUpdatedEvent', (data) => {
console.debug('Received branch update event!');
console.debug('Data: ', data);
debugger;
});
console.debug('Events subscribed!');
}
catch (error) {
console.error('Error when trying to setup signalr: ', error);
debugger;
}
- Hacky way by using internal services of Azure Devops (e.g. PullRequestUpdatesService, probably not the best way)
I tried to register my own callback, yet it is not called, when the event fires the older callback registered by Azure Devops is called (there seems to be some kind of closure).
let pullRequestUpdateService = await SDK.getService("ms.vss-code-web.pr-updates-service");
let oldFunction = pullRequestUpdateService.callbacks.onTitleDescriptionUpdated;
pullRequestUpdateService.callbacks.onTitleDescriptionUpdated = (e: any) => {
oldFunction(e);
console.debug("onTitleDescriptionUpdated called!");
console.debug("data:", e);
debugger;
};