3

I wrote the simple chat application using plain nodejs and Websocket module. Everything works fine, but if the page refreshed connection state changes to closed and description says that Remote peer is going away. I know that it standard code of RFC 6455, but why is my connection not updating with the page so the chat continue to work. How to handle page refreshing on client side?

1 Answer 1

7

Refreshing a page in a browser, closes and releases ALL resources associated with the original page and then loads a fresh copy of the page and runs any Javascript in the page again.

So, if your page initialization code includes opening a webSocket to your server, then that webSocket will be closed when the page is reloaded.

This is what will happen upon the initial page load and then a refresh:

  1. User requests your page
  2. Browser loads HTML for that page
  3. Browser runs Javascript in that page
  4. That Javascript creates a webSocket connection to your server
  5. User presses refresh
  6. Browser closes down all resources associated with the original page, including closes the webSocket connection.
  7. Browser reloads the original HTML for the page
  8. Browser runs Javascript in that page again
  9. That Javascript creates a new webSocket connection to your server

How to handle page refreshing on client side?

After refresh, the browser will run the Javascript in your page and that Javascript should just open a new webSocket connection and should establish a new chat connection.


FYI, that code you referenced has at least one bug. For example, the server-side code that removes a client from the server-side index does not work properly. It remembers the index for when the client connection was added to the array and assumes that the index never changes, but that's just wrong. As clients are added/removed from the array and they connect/disconnect that index can change. You're going to have to fix/debug that code if you use it.

While it can be made to work using an Array, I would probably use a Set myself and then it would be easier to remove an item too.

Sign up to request clarification or add additional context in comments.

9 Comments

I think, I should specify the situation. I load two localhosts and starting to chating. After I send a message and reload the page from which message was sent, other page connection changes to closed. How to fix this?
@Абдулла - Sorry, can't really help you further without seeing the actual code involved. Each page should re-establish a webSocket connection AFTER it is refreshed. And, then perhaps your server needs to connect them back together - I have no idea how your code in your server works in this regard.
My app based on this post I just changed all function to async, but the structure same: medium.com/@martin.sikora/…
@Абдулла - So, what doesn't work when the user refreshes the page? What exactly happens? What are you expecting to happen?
@Абдулла - FYI, that code you referenced has bugs. For example, the server-side code that removes a client from the server-side index does not work properly. It remembers the index for when the client connection was added to the array and assumes that the index never changes, but that's just wrong. As clients are added/removed from the array and they connect/disconnect that index can change. You're going to have to fix/debug that code if you use it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.