diff --git a/plugins/signals-node/src/SignalService.ts b/plugins/signals-node/src/SignalService.ts index 54f09f583a..9c621d5cbd 100644 --- a/plugins/signals-node/src/SignalService.ts +++ b/plugins/signals-node/src/SignalService.ts @@ -37,7 +37,6 @@ import { LoggerService } from '@backstage/backend-plugin-api'; /** @public */ export class SignalService implements EventSubscriber { - private readonly serverId: string; private connections: Map = new Map< string, SignalConnection @@ -58,7 +57,6 @@ export class SignalService implements EventSubscriber { identity: this.identity, } = options); - this.serverId = uuid(); this.server = new WebSocketServer({ noServer: true, clientTracking: false, @@ -199,6 +197,21 @@ export class SignalService implements EventSubscriber { return; } + // If there is event broker, use that to publish the message to + // all signal services, including this one. + if (this.eventBroker && !brokedEvent) { + await this.eventBroker.publish({ + topic: 'signals', + eventPayload: { + recipients, + message, + topic, + }, + }); + return; + } + + // Actual websocket message sending this.connections.forEach(conn => { if (!conn.subscriptions.has(topic)) { return; @@ -217,29 +230,10 @@ export class SignalService implements EventSubscriber { conn.ws.send(jsonMessage); }); - - // If this event has not been broadcasted to all servers, then use - // EventBroker to do that - if (this.eventBroker && !brokedEvent) { - await this.eventBroker.publish({ - topic: 'signals', - eventPayload: { - recipients, - message, - topic, - }, - metadata: { server: this.serverId }, - }); - } } async onEvent(params: EventParams): Promise { - const { eventPayload, metadata } = params; - // Discard message from same server to prevent duplicate messages - if (!metadata?.server || metadata.server === this.serverId) { - return; - } - + const { eventPayload } = params; if ( !eventPayload?.recipients || !eventPayload.topic || diff --git a/plugins/signals-react/src/hooks/useSignalApi.ts b/plugins/signals-react/src/hooks/useSignalApi.ts index bb6f47a5fe..8534732264 100644 --- a/plugins/signals-react/src/hooks/useSignalApi.ts +++ b/plugins/signals-react/src/hooks/useSignalApi.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import { signalApiRef } from '../api'; -import { useApi } from '@backstage/core-plugin-api'; +import { useApiHolder } from '@backstage/core-plugin-api'; import { JsonObject } from '@backstage/types'; import { useEffect, useState } from 'react'; @@ -23,10 +23,13 @@ export const useSignalApi = ( topic: string, onMessage: (message: JsonObject) => void, ) => { - const signals = useApi(signalApiRef); + const apiHolder = useApiHolder(); + // Use apiHolder instead useApi in case signalApi is not available in the + // backstage instance this is used + const signals = apiHolder.get(signalApiRef); const [subscription, setSubscription] = useState(null); useEffect(() => { - if (!subscription) { + if (signals && !subscription) { const sub = signals.subscribe(topic, onMessage); setSubscription(sub); } @@ -34,7 +37,7 @@ export const useSignalApi = ( useEffect(() => { return () => { - if (subscription) { + if (signals && subscription) { signals.unsubscribe(subscription); } };