node.js connection will be closed if the user changed the page - node.js

i have progged a community.
At the Login.php i connect and listen via
var socket = io.connect("http://ajkfh.com:8080");
socket.on("connect", function() { ......
but then, if the user changed on another site from the community, his connection will be closed and he is not longer listen.
how can i hold open the connection over underlying sites of the community. THANKS and sorry for my bad english

A socket connection is available only when the user remains on the page. There is no way for you to make a socket connection available across page loads. What you can do, is emulate page loads into ajax requests which load content inside a container.
Or you could just re-establish connection on page load, and have a mechanism to keep a track of clients (storing a client ID on a cookie or something)

Seemed you use socket.io, which is websocket. The websocket connection does lost if the page refreshed or closed.
If you want to retain something between pages, use Cookie or Session, just like the normal http way. And on browser side, you can read the data out(e.g. via the document.cookie or some lib) and send back to the server over websocket.

Related

Persistent socket with mongoDB and socket.io

We are facing a problem where clients connect to the server and a socket is created and is working.
But for example if the server goes down / we upgrade the version and restart the server,
the sockets are lost until clients refresh the page.
Our clients are passive, and will not refresh the page often leaving the server disconnected for updates and notifications.
We would like to know if it is possible to store the sessions somehow like a mongo store.

Is each browser tab its own connection when using Socket.io?

Say there are 5 open tabs in a single browser, and I am using Node.js and Socket.io. The client and server exchange packets to maintain (establish) communications.
Will connection with the client be lost if of one tab is closed?
How can I determine if the user closed the browser?
Yes, socket.io establishes a new connection with every tab. You're going to want to look into using session cookies to figure out which user the current socket.io connection is communicating with.

How to scrape socket.io updates to a third-party site?

I basically want to know if its possible to use Socket.io using the server-side only with no client side? BUT I want to know if my server-side can instead connect with a different site that I cannot use Socket.io to connect to.
Use PhantomJS to load the third-party site and then inject your own javascript into the page to catch events and send those events back to your own server.
socket.io is a two-way connection. Client <--> Server. You must have a socket.io endpoint at both ends to even establish a connection in the first place. And, then once you establish the connection, you must have agreed upon messages that can be exchanged between the two ends for it to do anything useful.
It is not useful to have a server-side socket.io that doesn't actually connect to anything and nothing connects to it. It wouldn't be doing anything, just sitting there waiting for someone to connect to it.
It is possible to have two cooperating servers connect to one another with socket.io (one server just acts like a client in that case by initiating the connection to the other server). But, again both endpoints must participate in the connection for the connection to even be established and certainly for it to do anything useful.
If you just want to download the contents of a site for scraping purposes, then you would not use socket.io for that. You would just use the nodejs http module (or any of several other modules built on top of it). Your server would essentially pretend to be a browser. It would request a web page from any random web server using HTTP (not socket.io). That web server would return the web page via the normal HTTP request. Your receiving server can then do whatever it wants with that web page (scrape it, whatever).

WebSocket Connection establishment from Browser

The question is regarding Web Socket connection establishment procedure.
from RFC 6455 , i understand that WebSocket technology was developed for browser based applications to establish full duplex TCP connection with the server.
My questions,
So when we Say browser based , the only way to establish web Socket connection is using javaScripts? i.e all browser based clients can establish webSocket connection using JS?
Can we use the WebSockets URL to render webPage on the borwser? Does browsers supports?
Like typing ws://www.sample.com/login in the address bar will display login page?
Does browser understands "ws" as protocol and establishes connection and display the page?
So for my question2 i understand as, to establish a WebSocket connection from Browser, We should already have a webPage and logic in that webPage will establish the WebSocket connection. Please correct me if i am wrong.
Thanks
Pradeep
For the WebSocket API, the client-side code must be JavaScript, yes. The server-side code can pretty much be whatever language you want.
To answer your other question, the WebSocket protocols (both ws and wss) cannot be used to load a web page directly. The WebSocket protocols can only be used to establish a connection with a server-side script, which upon a successful handshake, will upgrade the HTTP connection to a WebSocket connection to reduce the headers sent between the client and server.
So, yes, in general, you should already have a web page coded separately, and then add the WebSocket logic on top of that to establish a socket connection with the server as necessary.

How to make connection via SockJS (websocket) with NodeJS durable/persistent?

What is best solution to manage connection in NodeJS connected via SockJS (websocket) to be durable(do not disconnect) for some time during user reloads the page?
Can I do that with session?
SockJS, native websockets and all resources that the browser maintains are released when the user reloads (or leaves) the web page. Cache, localstorage (and friends) and cookies are notable exceptions.
If you wish to have a SockJS connection when a page is open - well, you need to establish a connection when the page is opened.
SockJS is pretty fast and apparently it works fine even if you have loads of short-lived connections.
(BTW, take a look at web workers with websockets)

Resources