I
I
Ivan Molodov2016-05-30 21:20:04
Node.js
Ivan Molodov, 2016-05-30 21:20:04

Node JS + Socket.IO socket management with localStorage?

Good evening to all who read this question. I decided to write a thesis on Node + Socket.io. The bottom line is to create a service similar to SoundCloud. I decided to use sockets for the rapid exchange of data between the server and the client and (well, to show on defense that I didn’t just spend 4 years at the university). The bottom line is that this service can be used by both authorized and non-authorized users (with different functionality). I wanted to do most of the information exchange on sockets and then a question surfaced. When a user opens or reloads a page, a new socket is created, but functionality needs to be created that will allow the server to distinguish between different users' sockets. To do this, each user at the first login to the site in localStorage stores a key, which is the id of the socket under which he entered, as well as an array of sockets that relate to this user + a field that counts the number of sockets. Everything would be fine and the server has an array in which a similar array of sockets is formed using this key, but the problem is that, as I said at the beginning, when the page is reloaded, the socket closes and a new one is created, but it is in localStorage and it must be removed there. In general, the problem here is how to manage the user's active sockets both on the server and on the client. The 'disconnect' event doesn't seem to fire on the client, because the page is reloaded and the script doesn't have time to fire. There was an idea after reloading the page at the Onload event to call a function through a timeout that will compare the data on the server and on the client, but in my opinion this is stupid and after this I will be given his picture instead of a diploma, and secondly, this method will not work if you close all the tabs of the site in general. Hope for understanding. Immediately I apologize for the grammar, my hands are shaking, the pre-defense is breathing in my back. If someone wants to suggest a session mechanism, please just explain how to conduct a session with unauthorized users. I warn you right away, I'm new to this and I don't understand a lot of things. Please don't get angry and talk to the point.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2016-05-30
@taliban

identify the client not by the socket, but by the identifier, let it be the ID of the first socket, write it down for yourself and constantly send it as a user ID on a new connection, the socket is just a connection.
As a result, with each new connection, the same ID will come to you from one person, and work with it.

K
kuzin_mv, 2016-06-01
@kuzin_mv

how to manage active user and server sockets

Maybe this example will help you https://github.com/kuzinmv/node-cluster-socket.io/... If you do not have a UserId for unauthorized clients, you can use the session Id, for example from here https://github .com/expressjs/session from this or a similar middleware if you have Express.js. Or generate some id yourself on the server and put it in the cookie.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question