mean-expert-official / loopback-component-realtime

The LoopBack Component that turns this great framework into a powerful real-time platform
MIT License
30 stars 17 forks source link

Unhandled error for request GET /socket.io/... #20

Closed jupham closed 7 years ago

jupham commented 7 years ago

I'm just starting out with the realtime component and I'm having a little issue getting setup. After setting up my loopback project and getting that running I put in the changes listed under "Setup Back End Module" but when I run the server I keep getting Unhandled error for request GET /socket.io/?EIO=3&transport=polling&t=LUqK942&b64=1 or some variation of that.

I'm not sure what I'm missing but any help on this would be appreciated.

p.s. The error comes every few seconds and if I take out the loopback-component-realtime config they go away.

jonathan-casarrubias commented 7 years ago

@jupham there is a missing step and I'm sorry for that, I will add that ASAP.

Basically the issue is that the client application is trying to send a heartbeat to the server and this last one is not being able to send a response.

You need to update the server/server.js file as follows:

var loopback = require('loopback');
var boot = require('loopback-boot');

var app = module.exports = loopback();

app.start = function() {
  // start the web server
  return server = app.listen(function() {
    app.emit('started', server);
    var baseUrl = app.get('url').replace(/\/$/, '');
    console.log('Web server listening at: %s', baseUrl);
    if (app.get('loopback-component-explorer')) {
      var explorerPath = app.get('loopback-component-explorer').mountPath;
      console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
    }
  });
};

// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname, function(err) {
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

Basically only 2 lines are changed

  return app.listen(function() {
    app.emit('started');

For

  return server = app.listen(function() {
    app.emit('started', server);

With this you should be able to be up and ready.

Cheers Jon

jonathan-casarrubias commented 7 years ago

I just added this to the documentation, thanks for contributing with this issue.

Cheers! Jon

jupham commented 7 years ago

Running the changes you submitted gave me a 'ReferenceError: server is not defined' for the line return server = app.listen(function() {

I can get past it by just putting in var server; before the return but I was wondering where your server declaration is coming from.

Thanks, Jordan

jonathan-casarrubias commented 7 years ago

@jupham I usually don't receive an issue, but you can definitely define the var server right before. What is crucial is to pass the reference within the started event, so the real time module can receive it at the right moment.

I believe this would be the most healthy

app.start = function() {
  // start the web server
  var server = app.listen(function() {
    app.emit('started', server);
    var baseUrl = app.get('url').replace(/\/$/, '');
    console.log('Web server listening at: %s', baseUrl);
    if (app.get('loopback-component-explorer')) {
      var explorerPath = app.get('loopback-component-explorer').mountPath;
      console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
    }
  });
  return server;
};