Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primus doesn't accept custom path with engine.io #774

Closed
balakrishna222111 opened this issue Jul 8, 2021 · 10 comments
Closed

primus doesn't accept custom path with engine.io #774

balakrishna222111 opened this issue Jul 8, 2021 · 10 comments

Comments

@balakrishna222111
Copy link

5.1.2

Environment:

  • Linux:
  • chrome:
  • express.js:

Expected result:
"https://app.application.com/mycustompath/?_primuscb=*******************"

Actual result:
"https://app.application.com/?_primuscb=*******************"

Steps to reproduce:

`var server = http.createServer(app);
var Primus = require('primus');
const Session = require('server/session');
var primus = new Primus(server, {
transformer: 'engine.io',
pathname:"/mycustompath/primus",
compression: true,
transport: {
cookie: Session.getEngineIOCookieName()
}
});

var Connection = require('server/connection');
primus.on('connection', function (spark) {
Connection.create(spark);
});

primus.on('disconnection', function (spark) {
Connection.destroy(spark);
});

/**

  • Listen on provided port, on all network interfaces.
    */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);`

@lpinca
Copy link
Member

lpinca commented Jul 13, 2021

I cannot reproduce the issue:

const Primus = require('primus');
const http = require('http');
const fs = require('fs');

const server = http.createServer(function (req, res) {
  res.setHeader('Content-Type', 'text/html');
  fs.createReadStream(__dirname + '/index.html').pipe(res);
});

const primus = new Primus(server, {
  pathname: '/mycustompath/primus',
  transformer: 'engine.io'
});

primus.on('connection', function (spark) {
  console.log(spark.id, 'connected');
});

server.listen(3000, function () {
  console.log('listening on port 3000');
});
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <script src="/mycustompath/primus/primus.js"></script>
    <script>
      (function () {
        var primus = new Primus({ strategy: false });

        primus.on('open', function open() {
          console.log('open');
        });
      })();
    </script>
  </body>
</html>

Immagine 2021-07-13 111533

Did you rebuild the client?

@lpinca
Copy link
Member

lpinca commented Jul 13, 2021

It does not matter, that is just where the Primus client is found.

const Primus = require('primus');
const http = require('http');
const fs = require('fs');

const server = http.createServer(function (req, res) {
  if (req.url === '/primus-client.js') {
    res.setHeader('Content-Type', 'text/javascript');
    fs.createReadStream(__dirname + '/primus-client.js').pipe(res);
    return;
  }

  res.setHeader('Content-Type', 'text/html');
  fs.createReadStream(__dirname + '/index.html').pipe(res);
});

const primus = new Primus(server, {
  pathname: '/mycustompath/primus',
  transformer: 'engine.io'
});

primus.save(__dirname + '/primus-client.js');

primus.on('connection', function (spark) {
  console.log(spark.id, 'connected');
});

server.listen(3000, function () {
  console.log('listening on port 3000');
});
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <script src="/primus-client.js"></script>
    <script>
      (function () {
        var primus = new Primus({ strategy: false });

        primus.on('open', function open() {
          console.log('open');
        });
      })();
    </script>
  </body>
</html>

@lpinca
Copy link
Member

lpinca commented Jul 13, 2021

It means the Primus constructor is not found. Ensure that the client script it correctly loaded.

@lpinca
Copy link
Member

lpinca commented Jul 13, 2021

I can't debug your code, sorry. That's up to you.

@lpinca
Copy link
Member

lpinca commented Jul 13, 2021

You probably just have to save the client lib under the javascript folder and get it from there.

primus.save('/path/to/javascript/primus.js');
<script src="/path/to/javascript/primus.js"></script>

@lpinca
Copy link
Member

lpinca commented Jul 13, 2021

That is a CORS issue because you are sending a cross origin request from https://app.autoscheduler.ai to https://api.autoscheduler.ai. See https://github.com/primus/primus#getting-started and https://github.com/socketio/engine.io#methods-1.

@lpinca
Copy link
Member

lpinca commented Jul 14, 2021

Requests for primus/engine.io are not handled by express.

@lpinca
Copy link
Member

lpinca commented Jul 14, 2021

No, server side.

@lpinca
Copy link
Member

lpinca commented Jul 14, 2021

No, cors is not a valid primus option. It should eventually be origins but it depends on your architecture. Is https://api.autoscheduler.ai/ running primus? From the screenshots it seems to run a plain engine.io server. If so you should use the cors option as per https://github.com/socketio/engine.io#methods-1 and things might not work as you expect if you use a primus client to connect to it.

@lpinca
Copy link
Member

lpinca commented Jul 14, 2021

I'm closing this. Let me know if it should stay open.

@lpinca lpinca closed this as completed Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants