2

I have a bit of an issue. I'm trying to create a dynamic web app using node.js/express.js/now.js. I've done everything as shown in the small sample code at http://nowjs.com/download , with no success, the client-side now.js script hosted properly, but now.ready(..) never fires. The only differences are that I use express and my server which is used to initialze now.js is https.

Do you have any ideas which could cause it not to work?

server side:

var server = express.createServer(..);
..
server.listen(port, function() {..});
var nowjs = require('now');
var everyone = nowjs.initialize(server);
everyone.now.log = function(msg) { console.log(msg); }

client side:

<script type="text/javascript" src="/nowjs/now.js"></script>
<script type="text/javascript">
    now.ready(function() { now.log('asd'); alert('asd'); });
</script>

Any help would be highly appreciated!

Best, Kornel

kataik
  • 510
  • 1
  • 5
  • 17

2 Answers2

3

Well, found the answer. Long answer: now.js has an issue when determining the communication port on which socket.io should communicate. This issue seems only to appear when using default https port (443).

I've found two solutions, the ugly one: https://groups.google.com/forum/?fromgroups=#!topic/nowjs/8cO9D77tN2o

Basically you need to edit the source code of now.js at now/lib/fileServer.js and replace

var hostPort =  options['port'] || host[1] || '80';

with

var hostPort =  options['port'] || host[1] || ((request.headers.referer.split(':')[0] === 'https') ? '443' : '80');

The nicer one is to set port options to socket.io. Lucky us, this is supported by now.js:

var everyone = nowjs.initialize(server, {port: port, socketio: {transports: ['xhr-polling', 'jsonp-polling']}});

I hope that this will help others having the same issue and also hope that this behavior will be fixed later in now.js.

Best regards: Kornel

kataik
  • 510
  • 1
  • 5
  • 17
0

Running latest version of node and now on OSX, with Safari.

server.js

var html = require('fs').readFileSync(__dirname+'/index.html');
var httpServer = require('http').createServer(function(req, response) { 
    /* Serve your static files */ 
    response.end(html);
})
httpServer.listen(8080);

var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);

console.log('done');
everyone.now.logStuff = function(msg){
    console.log(msg);
}

index.html

<script type="text/javascript" src="http://localhost:8080/nowjs/now.js"></script>

<script type="text/javascript">
  now.ready(function(){
    // "Hello World!" will print on server
    now.logStuff("Hello World!");
  });
</script>
done..

Start the server:

node server.js

Open your browser:

http://localhost:8080
Roger
  • 7,535
  • 5
  • 41
  • 63
  • These two represents different semantics. $(document).ready(..) means that the html dom has been loaded. now.ready(..) means that the socket.io connection has been established to the server. I've tried tough, still not work with express.js and htmls... :(( – kataik Aug 22 '12 at 07:25
  • This sample works as expected, still, it doesn't use express and is not an https server. – kataik Aug 22 '12 at 08:02
  • "NowJS 0.7 supports passing in an HTTPS server upon initialization. No additional configuration is required." http://blog.nowjs.com/ – kataik Aug 22 '12 at 08:04
  • Well try my example with just https or express. So you can isolate the problem. I don't have https setup. – Roger Aug 22 '12 at 08:05
  • well for self-signed certs i can imagine it might bring problems. But that the express doesn't work. Can you post the full code? – Roger Aug 22 '12 at 08:59
  • thank you for your help, I could post a sample, but I've found the source of the problem just now. I'll write it down in a moment... – kataik Aug 22 '12 at 09:16
  • ok, great. you can answer your own question! so it doesn't stay unanswered. – Roger Aug 22 '12 at 09:19