Flotype / now

NowJS makes it easy to build real-time web apps using JavaScript
http://www.nowjs.com
MIT License
1.91k stars 175 forks source link

now.core.on("ready",..) does not fire when there is a lot of HTML in the page. #84

Closed felixchan closed 13 years ago

felixchan commented 13 years ago

I'm building a very basic chat room.

When the page loads, I first run a for loop to spit out the previous messages (which I queried on the server side.)

When there is a lot of messages (a lot of HTML), now.core.on("ready") sometimes does not get triggered. Even if I wait for 5 minutes.

Sometimes I have to refresh the page , and 50% chance it will work.

When the number of messages is short (< 500), now.core.on("ready") always gets triggered and works perfectly.

Notes: -I have little javascript. That now.core.on("ready") only does 1 thing, which is now.joinRoom("string"). Of course, that's inside JQuery document.ready and it's also one of the first things. -When the page loads, regardless of whether now.core.on("ready") gets triggered, my node.js log always says:

Initializing client with transport "websocket" Client 8420563354156911 connected

And also, websocket /socket.io is always "pending" 101 , regardless if it was triggered or not.

ericz commented 13 years ago

Hi felixchan,

Try using now.ready, instead of now.core.on.

The issue, I believe, is that if the server handshake is completed before now.core.on is called, the callback you pass in does not in fact get called.

However now.ready is an alias to now.core.on('ready') with the exception that it calls the callback immediately if now is already ready.

ericz commented 13 years ago

Judging from the fact that you seem to experience the problem with large HTML files, (takes longer for $(document).ready()), I would definitely say the issue is as I described above.

I don't think I'll change now.core.on('ready') behavior to call passed in functions immediately if the client is already ready because that would not follow the event emitter model. Instead, please use now.ready which does have that behavior

adityab commented 12 years ago

@ericz , I'm finding this issue still holds, even when using now.ready. I also had a lot of HTML and jQuery DOM manipulations going on.

The server logs the following:

warn client not handshaken client should reconnect

Looks like a socket.io message to me.

I've found a temporary workaround by forcing xhr-polling, but none of the other methods (including the default invocation without parameters) for nowjs.initialize() work.

Hope this helps.

Edit: I wrote an app using Now.js - https://github.com/adityab/memecached that shows this problem - strangely enough, it works fine on localhost, but exhibits said problem from my EC2 instance.

See this line https://github.com/adityab/memecached/blob/master/app.js#L18 for the workaround.

steveWang commented 12 years ago

now.ready and now.core.on('ready') are functionally equivalent, IIRC. :D

I imagine that the issue is the enormous amount of data to transfer -- this presumably causes the handshake to timeout, since the queue is full. This probably also has to do with why it works just fine on localhost: you don't actually have propagation delays to worry about.

You can probably solve this by setting autoHost to false and placing a call to nowInitialize inside a $(document).ready block, sort of like the following:

$(document).ready(function () {
  window.now = nowInitialize('http://myUrl');
  now.func = function () {};
  now.ready(function () {
    console.log('This should work.');
  });
});