React Native Signalr keeps saying connection needs to be initialized

Viewed 90

I have this in my code:

useEffect(() => {

    if(loaded2 == false){

        connection = signalr.hubConnection('https://webambe.com/xxx');
        connection.logging = true;
    
        proxy = connection.createHubProxy('xxx');
        //receives broadcast messages from a hub function, called "helloApp"
        proxy.on('updateAll', (argOne) => {
            
            setAlertNum(1);

          //Here I could respond by calling something else on the server...
        });
        // atempt connection, and handle errors
        connection.start().done(() => {
            //console.log("dddd" + proxy.connectionState);
            setLoaded2(true);
          //console.log('Now connected, connection ID=' + connection.id);
          setTrans(false);
    
          
        }).fail(() => {
          //console.log('Failed');
        });
    
        //connection-handling
        connection.connectionSlow(() => {
          //console.log('We are currently experiencing difficulties with the connection.')
        });
    
        connection.error((error) => {
          const errorMessage = error.message;
          let detailedError = '';
          if (error.source && error.source._response) {
            detailedError = error.source._response;
          }
          if (detailedError === 'An SSL error has occurred and a secure connection to the server cannot be made.') {
            //console.log('When using react-native-signalr on ios with http remember to enable http in App Transport Security https://github.com/olofd/react-native-signalr/issues/14')
          }
          //console.debug('SignalR error: ' + errorMessage, detailedError)
        });

    }
});

When the code hits connection.start().done((), then the connection should be established but when I hit the send button that invokes the hub i get this error (setLoaded2(true) means that the connection is established):

Connection has not been fully initialized. Use .start().done() to run the logic after 
connection has started.

But I have stated that says that connection has started here:

setLoaded2(true);

When this is called setTrans(false) is called to get rid of the transparent view so users can interact with the UI.

The hub connects successfully and I can run commands to the server and back, but I just want to know how to say when the connection is established so that the user can use the system.

1 Answers

I'm not sure if this is the way it shud be done but its working and staying connected when I move the connection and proxy outside my functional component:

    connection = signalr.hubConnection('https://webambe.com/xxx');
    connection.logging = true;

    proxy = connection.createHubProxy('xxx');

when added this code outside the component the connection stays connected and no issues with signalr

Related