Search code examples
node.jssocket.iopage-refreshweb-worker

Do page refreshes defeat the the use of NodeJS/Socket.IO and cause too much drain on a server in creating new connections?


In a current web application, where the UI cannot be changed to accommodate page section refreshes rather than an entire page refresh (linking to other pages, etc.). Eventually this would be placed in a non-updating div, however for now, page links will destroy/create the web worker, forcing a new socket connection to be created. What is the drain on the system when having to create new socket connections using Socket.IO?

Given that the entire page will refresh, is this still a good solution?

-- UPDATE --

The application for this is for a system-based push notifications like "friend" logins, etc. I see this as something similar to the private message in a chat environment. I would want to broadcast these events, and the client side would manage who actually gets to see the updates. Does sound like the right way of doing it?


Solution

  • There are a lot of factors at play here. I'll list a few of them:

    1. What browser is the user using and what is their system like?

      Some browsers and slower machines will take require more overhead to open the socket. That said, this should be pretty marginal with any modern machine and connection.

    2. What transports do you have enabled?

      You can configure Socket.IO to use many different types of transports, including WebSocket, XHR polling, JSONP polling, and even Flash. Which transport actually gets used will be based on what you have configured and on what the user's browser supports.

      Some of these transports, such as Flash (disabled by default), will obviously require significantly more overhead to setup. Others, like XHR polling, are inherently inefficient, but are largely unaffected by new page requests since you are making multiple poll requests anyway.

    3. What are you doing when a Socket.IO connection is established, and when a disconnect occurs?

      If you have heavy crunching that happens in either of these scenarios, then frequent reconnects are going to be a problem. That said, you really shouldn't be doing heavy crunching on connects, since that pretty much kills XHR polling.

    4. How frequently are these page refreshes happening?

      Is it only occasionally, when a user clicks a link? Or do you have something that causes the page to refresh extremely frequently?

    5. What kind of hardware and connection do your servers have?

      This is a pretty big variable that is hard to fix down.

    While I can't give you a definitive answer, hopefully this will help you think about and optimize your scenario. In general, creating new sockets fairly frequently should not be an issue, but I'd recommend performing load or stress testing to see what your particular system can handle.