Persistent connections across pages ....

#1

Hi, I’ve been struggling with the concept of maintaining a WAMP connection within a website when the user switches between pages.
For greenfield projects it’s easy, just write a single page application and mimic links and page switches, but that doesn’t do much for pre-existing web sites.

The best I’ve come up with thus far is to load a dummy page with autobahn, make a websocket connection to crossbar, then run the “actual” website in an iFrame.

(then I can access the WAMP connection from within the website by referencing the iframe parent)

Need to be a little careful with how the iFrame is handled but it seems to work, and I’ve managed to implement this mechanism as a Wordpress plugin (as Wordpress accounts for most of the sites I work with)

  • this seems to work fairly well, but it “feels” like a kludge.

Workers look interesting, but support seems incomplete in different browsers and looking at the forums, maybe a little buggy.

Anyone got any recommendations / pointers?

(I did look at the page on the crossbar site, but it seemed a little inconclusive …)

0 Likes

#2

Hi Gareth!

Base on the feature request I made (https://www.w3.org/Bugs/Public/show_bug.cgi?id=24691), the WHATWG suggests that browsers let service workers survive the page a bit, so that a subsequent page from the same domain can claim it.

The WHATWG link from the above request currently leads to a security warning for me, so I can’t check whether this is still in the spec - and I haven’t tested whether this is implemented anywhere. I think that this would be the best solution.

Regards,

Alex

0 Likes

#3

Yeah, apparently there are two types of workers, and the persistent variety is only supported on a couple of browsers, and again only recent versions. Any solution based on workers looks like being a few years away from being “compatible” with the average machine. (last time I looked on the support matrix there was no mobile support at all …) Just wondered if there was another solution out there somewhere …

0 Likes

#4

Ok, came to the conclusion Wordpress simply isn’t going to play nice with iFrames. Kept finding more special cases.

That said, I seem to have found an alternative, in this case it’s Wordpress specific, but the mechanism ‘should’ work with most legacy sites.
https://gareth.bult.co.uk/2017/01/13/persistent-wordpress/

Essentially, in the onload handler, catch all links (mostly A+HREF or FORM for Wordpress) and replace the actions with a script that loads the target via AJAX and overwrites the current document. (leaving global and standard web workers in-tact) Sounds a little taxing but the current code is only 20 odd lines …

0 Likes

#5

Hi, I’ve tried to formalise this a little and subject to things I’m not catering for, I think this is a sustainable solution with regards to “transparently” converting “legacy” websites effectively into single page applications with a view to maintaining persistent websocket connections.

Code is available here;

https://github.com/oddjobz/spa

I have installed on half a dozen sites at the moment running a persistent connection to my analytics tool, seems to be working Ok …

0 Likes