CraZyDroiD
CraZyDroiD

Reputation: 7115

Using SignalR in a Electron Net app without eventsource

I have an ASP.NET Core + ReactJS project which is wrapped with Electron Net. I want to use SignalR to communicate between the server and the frontend.

I have setup everything correctly. If I run my app on browser, everything works. But when I run it in Electron environment, I get an error:

Cannot find module 'eventsource'

Require stack: - electron/js2c/renderer_init

I can get it to work if i set NodeIntegation to false. But I need it to be true.

This is how I establish the connection in my frontend:

useEffect(() => {
    // setting up singalR Connection to listen to updates from server.
    let connection: signalR.HubConnection
    const connectToHub = async () => {
      try {
        connection = new signalR.HubConnectionBuilder()
          .withUrl('http://127.0.0.1:5293/progressHub', {
            withCredentials: true,
            skipNegotiation: true,
            transport: signalR.HttpTransportType.WebSockets,
          })
          .build()

        connection.on('ReceiveMessage', (title, message) => {
          ServiceManagerInstance.progressIndicatorService.setIndicator(title, message)
        })
        await connection.start()
        console.log('SignalR Connection Started')
      } catch (error) {
        if (error instanceof Error) {
          console.error('SignalR Connection Start Error:', error.message)
        }
      }
    }

    connectToHub()

    return () => {
      if (connection && connection.state === signalR.HubConnectionState.Connected) {
        connection.stop()
        console.log('SignalR Connection Stopped')
      }
    }
  }, [])

I have tried adding

skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets,

as it was suggested somewhere. But I still have the same issue.

Has anyone else encountered this issue? How can I fix this issue? Thanks.

Upvotes: 0

Views: 76

Answers (0)

Related Questions