olofd / react-native-signalr

Use SignalR with React Native
150 stars 61 forks source link

Cannot set property 'createElement' of undefined #36

Closed prakashw3expert closed 7 years ago

prakashw3expert commented 7 years ago

I'm getting this issue when try to start connection in disconnected function.

Cannot set property 'createElement' of undefined

The issue is coming on this line of signalr index file

`hubConnectionFunc.start = (...args) => { window.document.createElement = () => { return { protocol, host }; }; window.location = { protocol, host }; const returnValue = originalStart.apply(hubConnectionFunc, args); window.document = undefined; return returnValue; };

I'm try to restart connection after disconnected and in app active state. My approach is that when app goes to backend, stop the connection and start connection again when app is active again.

Here is my code to integrate -

  let url = AppConfig.ApiUrl+'signalr';
   let qs = "AuthKey="+userData.authKey;
   const connection = this.connectionSignalR = signalr.hubConnection(url, {
      useDefaultPath: false,
      qs : qs
   });
   connection.logging = true;
   const proxy = connection.createHubProxy('notificationHub');
   //receives broadcast messages from a hub function, called "helloApp"
   proxy.on('realTimeNotify', (message) => {
      // Handling of the real time notification here

   });

  connection.disconnected(() =>  {
       setTimeout((connection) => {
            connection.start();
        }, 3000) // Restart connection after 3 seconds.

  });

  connection.start().done(() => {
    console.log("connection", connection);
    console.log('Now connected, connection ID=' + connection.id);
    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')
      }
      alert('SignalR error: ' + errorMessage+". "+detailedError);
      connection.stop();
    });
  }).fail(() => {
    console.log('Failed');
  });

  //connection-handling
  connection.connectionSlow(() => {
    console.log('We are currently experiencing difficulties with the connection.')
  });
}

_handleAppStateChange = (nextAppState) => { if(nextAppState === 'background' && this.connectionSignalR) { this.connectionSignalR.stop(); } else if(nextAppState === 'active' && this.connectionSignalR){ this.connectionSignalR.start(); } }`

emirsnck commented 7 years ago

I have the same problem +1

emirsnck commented 7 years ago

it may be related to this commit https://github.com/olofd/react-native-signalr/commit/95d8ca31ab3985099464187a6a1ed0def658370c

I will try again after updating

olofd commented 7 years ago

Should be fixed after PR.