Ionut Eugen
Ionut Eugen

Reputation: 501

Can browser javascript listen to a route/url?

So I know about node.js and how to make it listen to requests on a route, and how to make a request to that route from the browser, however I was wondering if the opposite was possible as well.

Take this scenario for example:


I have a database with a series of random items, that are rendered on a page. The requests to the database to get the items are made on the server side and the client just requests that data from the server when it needs to. However (for firebase at least) firebase offers us some events that get launched every time the database was updated.

Ok so I update the data on the server, however how can I tell the user

"Yo ! take this data instead"

when there is some new data vaiable.


And here I was thinking of some sort of listen function on that route, as I don't really have too much of an idea for how to do this. (maybe serve the html page again, though idk how good it would be to load the entire page again, just because mom decides to add milk to the shopping list ) ?

Upvotes: 0

Views: 558

Answers (1)

VarunPrashar
VarunPrashar

Reputation: 34

You have to use websockets for this. Whenever there is some change in the data you just have to emit an event which browser will be listening to and dynamically add/manipulate dom automatically without page refresh.

Another solution is you can make continuous hits to server after every 10-15 secs to check for new info and if it gets then operate accordingly.(This sol is not recommended)

Regards, Varun

Upvotes: 1

Related Questions