Confusion about now.js client-side file hosting

127 views
Skip to first unread message

Tom

unread,
Jun 3, 2011, 7:36:30 AM6/3/11
to no...@googlegroups.com
It has been said that this now.js file is dependent of the server, which is the main argument on why it has to be hosted dynamically.

I wonder though, what exactly is being manipulated for each server? If it's just a server IP or something, a relative path could be used instead.

Also if there really is no other workaround, it would be useful to create a simple build script so that people with autoHost: false (like me) can easily retrieve the latest version.

Thanks.

- Tom

Eric Zhang

unread,
Jun 3, 2011, 12:50:00 PM6/3/11
to no...@googlegroups.com
I think a build script is definitely the way to go. This is definitely an oft requested feature so I'll add it to our whiteboard of things to do


Eric

Tom

unread,
Jun 3, 2011, 1:11:25 PM6/3/11
to no...@googlegroups.com
Alright, but could you tell me what exactly is different for each server?

- Tom

2011/6/3 Eric Zhang <er...@nowjs.com>

Sridatta Thatipamala

unread,
Jun 3, 2011, 1:24:29 PM6/3/11
to no...@googlegroups.com
Tom,
The only thing different is the server's host and port information. In
the source file, these are represented by the tokens **SERVER** and
**PORT** (https://github.com/Flotype/now/blob/master/lib/now.js#L420).
They are later string-replaced.

There is an undocumented feature that you may use instead of a build
script. To generate the client-side libraries for your version of
NowJS, you can use nowjs.generateClientLibs().

The function signature is generateClientLibs(serverHost, serverPort,
exportPath). It takes a host and port and writes the generated
client-side file to exportPath (where exportPath is a valid relative
or absolute filesystem path).

So you could write a single-purpose script like this:
var nowjs = require('now');
nowjs.generateClientLibs('localhost', 8080, '/home/tom/');

Hope that helps.
--Sridatta

Tom

unread,
Jun 3, 2011, 1:26:07 PM6/3/11
to no...@googlegroups.com
Sridatta,

Why would you need to know about the server and port? Why not simply use relative  URLs? Eg. script(src='/dependency1.js');

Tom

2011/6/3 Sridatta Thatipamala <srid...@nowjs.com>

Travis Glines

unread,
Jun 3, 2011, 1:28:21 PM6/3/11
to no...@googlegroups.com
Tom,

I don't mean to interrupt but I'm going to guess so that you could be serving the javascript from one machine and have now.js connect to a different server.  (this helps a lot with scaling a realtime cluster etc)

Travis

Sridatta Thatipamala

unread,
Jun 3, 2011, 1:29:22 PM6/3/11
to no...@googlegroups.com
Important correction. You must specify the name of the file in
exportPath as well. So the proper sample script would be:

var nowjs = require('now');
nowjs.generateClientLibs('localhost', 8080, '/home/tom/clientlibs.js');

Also, Travis's reasoning is correct. Relative URLs would not work in
all cases and we don't want to make any assumptions.

Tom

unread,
Jun 3, 2011, 1:29:42 PM6/3/11
to no...@googlegroups.com
@Travis,

Fair guess, but that would only make sense if the server did not use it's own IP and port (which it does).

2011/6/3 Travis Glines <tra...@exipe.com>

Tom

unread,
Jun 3, 2011, 1:33:42 PM6/3/11
to no...@googlegroups.com
Sridatta,

Do you not already make this assumption by substituting the server's host and port for %SERVER% and %PORT%?

Tom

2011/6/3 Sridatta Thatipamala <srid...@nowjs.com>
Important correction. You must specify the name of the file in

Sridatta Thatipamala

unread,
Jun 3, 2011, 1:42:53 PM6/3/11
to no...@googlegroups.com
Tom,
In the absence of any configuration options, we assume that your
websockets server is the same as your statics server. However, by
including these tokens, we offer the flexibility for you to use
generateClientLib to put whatever host and port setup you want.
--Sridatta

Tom

unread,
Jun 3, 2011, 1:47:45 PM6/3/11
to no...@googlegroups.com
Alright.

From a conceptual point of view it would make more sense to me to define the server's address within the client-side file (and not on the server side).

Sounds like a matter of preference or difference in standards though, so I will just use that custom build command to statically host, merge and minify now.js.

Tom

2011/6/3 Sridatta Thatipamala <srid...@nowjs.com>
Tom,

Vladimir Dronnikov

unread,
Jun 3, 2011, 3:28:45 PM6/3/11
to no...@googlegroups.com
> From a conceptual point of view it would make more sense to me to define the
> server's address within the client-side file (and not on the server side).

IMHO, this is right. This would 1) allow to have multiple now.js
instances on the page; 2) fine grain security options; 3) draw
unobvious preparation of the file for vanilla static servers.

Best regards,
--Vladimir

Tom

unread,
Jun 5, 2011, 4:38:31 AM6/5/11
to no...@googlegroups.com
One more related question I have is, when setting autoHost to false, should I also host socket.io myself? If not, how can I disable now.js's hosting of socket.io?

I need to host all these client side libraries myself, as I have more than 10 independent client-side modules now, so I need to merge, minify and gzip these to speed up my application's load time as much as possible.

Tom

2011/6/3 Vladimir Dronnikov <dron...@gmail.com>

Eric Zhang

unread,
Jun 5, 2011, 1:10:15 PM6/5/11
to no...@googlegroups.com
Hey Tom,

Socket.io is hosted by the socket.io library itself and not NowJS so autoHost: false won't disable.

However NowJS will allow you to pass options through to socket.io , but there doesn't appear to be a socket.io option to disable file hosting in the latest 0.6

3rdEden may have more insight about that.



Eric

Tom

unread,
Jun 5, 2011, 1:16:44 PM6/5/11
to no...@googlegroups.com
You're right, I've got it pretty much covered now. The answer was not to request the file ;)

On a side note, could you maybe give my pull request a look?

Thanks a lot.

Tom

2011/6/5 Eric Zhang <er...@nowjs.com>

Eric Zhang

unread,
Jun 5, 2011, 1:25:18 PM6/5/11
to no...@googlegroups.com
I gotta run today but I'll check out your pull request Monday morning.


Eric
Reply all
Reply to author
Forward
0 new messages