Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the browser's window object from a nodejs application?

I'm trying to write a simple app to watch over files changes and automatically reload the updated code in the browser. I'm aware of the existance of livereload nodeamon and others, i just wanted to write my own. I've created the server, let it read the file i want to read, called the watcher that kills and restart the server when changes happen in the watched file. Last part: it should refresh the browser. how is this possible?

like image 395
Davide Brunetti Avatar asked Oct 24 '15 02:10

Davide Brunetti


3 Answers

As others have explained, the browser programming environment and thus window.location.reload() is completely separate from node.js so you cannot call that directly from node.js. Server-side code runs on the server in node.js. Client-side code in the browser runs in the browser only. The two may communicate via Ajax requests or messages sent over a webSocket connection, but they can't call code in each other directly.


For a browser to refresh based on something that changes on the server, there are two basic approaches.

  1. Javascript in the browser can regularly ask the server if it has anything new (usually with an Ajax call that passes a timestamp). If the server says there is something new since that timestamp, then the Javascript in the browser can request the new data/file. This "polling" approach is not particularly efficient because if the data doesn't change very often, then most of the requests for something new will just return that there is nothing new. It's also not friendly for battery life.

  2. The browser can make a lasting webSocket connection to the server. Then, when the server finds that something has changed on the server, it can just directly send a notification to each connected browser informing it that there is something new. All connected clients that receive this message can then make a normal Ajax call to retrieve the new data. Or, the new info can just be directly sent to the client over the webSocket. In either case, this direct notification is generally more efficient than the "polling" solution.

With the webSocket option, you will NOT want your server to restart because that will drop all webSocket connections. I'm not sure why you're currently restarting the server when something changes, but you will have to change that to use the webSocket solution.

like image 145
jfriend00 Avatar answered Sep 28 '22 12:09

jfriend00


NodeJS does not have a window variable that represents the global namespace. Instead it has a variable called global.

There is no browser, window or URL location in NodeJS.

It's a purely server side tool.

like image 43
Reactgular Avatar answered Sep 28 '22 12:09

Reactgular


In node.js you have process which is a node.js global object like window which is a browser global object.

For more info type process into the node.js shell.

like image 21
Alok Deshwal Avatar answered Sep 28 '22 14:09

Alok Deshwal