Search code examples
phpwebsocketphpwebsocket

Whether or not to use iframe in an application


I am creating an application that will use Web Sockets for a notification system. Is it better to have the application in an iframe with the Web Sockets in the parent so there isn't a new connection every time a page is loaded? Or maybe it should re-connect?

What are your thoughts?

If anyone has any other way in PHP to get push like notifications without sending a AJAX request every 10 seconds then let me know.

Thanks.


Solution

  • This is one of the options you have described.

    The problem with that option is that there wont be direct control over the content of that inner iframe, and you will need to implement push message window communication between parent of iframe, in order to be able to change iframe src attribute, in case someone will refresh parent page, and iframe should refresh to actual state, not initial page.
    Second problem, there will be no SEO at all. So your page wont be crawlerable by search engine robots. If SEO is important for your application - then this is not an option. In WebSockets, if you work with sessions, it is important to make session available for normal PHP script and WebSockets logic, in order to keep consistent access to data it self. PHP will make it not an easy task at all.

    You might consider Long Pull technique as well, as it allows to open one AJAX request and then get responses back, and this request can last some time but will eventually close and have to be reopened on client-side.

    Another option is to review actual application architecture, and think of single-page application. It have as well cons and pros.
    Good thing about it, is UX will be much higher. Response times as well as you will load less content and data.
    Pros are that it requires lots of development on front-end side in javascript. As well there is two major routes you can do single-page applications. Consistent and inconsistent. In first case you need to make sure that your back-end will server static html on refresh or just navigating to specific link, the same way as your single-page application would generate using java-script. Then it solves issues with SEO. While inconsistent approach, will just be purely on javascript (front-end), and will have issues with SEO.

    WebSockets usually used with single-page applications, for example Facebook Chat is great example of such. Or Google Talk while you are in Gmail account. They are not meant to be used with often refreshed pages, as Handshake process is a bit more heavy than normal HTTP request.