Reputation: 6928
In my application, there is a custom requirement to detect whether a URL is open in the browser, (i.e. it could be in another tab, another window etc.). Is there a way to detect the same?
It can be in a rails way or using client side scripts.
I'm not sure its a good question or not.
Upvotes: 0
Views: 102
Reputation: 76784
The only contribution I could make would be about how it could work:
Requests
Because Rails works on requests, I would suggest the only way to "know" whether a certain URL is "open" is to use a perpetual connection (WebSocket or long-polling)
By using a websocket, or long-polling, you'd have a constant reminder of who's requesting your controller actions, thus providing you with information on which are open
Connection
If you're using websocket or long-polling technology, you could do it like this:
#app/controllers/your_controller.rb
def your_action
#can accept xhr requests
respond_to do |format|
format.html
format.js { render nothing #xhr request}
end
end
#app/assets/application.js
if (!!window.EventSource) {
var source = new EventSource(window.location);
}
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
This will continually request the page you're on with SSE
's (essentially using long-polling), thus allowing you to see if the action is open
This is super sketchy, but hopefully it will give you some ideas
Upvotes: 1