A
A
Andrew2019-10-14 15:54:31
Express.js
Andrew, 2019-10-14 15:54:31

User authentication in websocket (vue, express)?

Hey!
Has anyone here worked with such a bunch - Vue.js, express.js and WebSocket?
From the curvature of third-party plugins, I want to use the ws standard library.
The main question arises on the topic of identification. How does the server understand that a notification needs to be sent to a specific socket client? When connecting, clients each time have their own ID, which is generated automatically (at least in socket.io it was like that). I never figured out how to put a custom value into the socket object on the client.
And it's not entirely clear about the rooms, but this is less important now (but a link to a good article on this topic would be appreciated).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WebDev, 2019-10-14
@AndrewRusinas

Websockets, like http, is a stateless protocol. That is, after reloading the page, the server does not know whether you are or not.
Therefore, after connecting to websockets, you need to send some kind of identifier to the server, for example, your id.
Store connections and user data on the server. When you need to send a message, find the desired connection on the server and send data to it.
Here is a good example of a simple websocket chat. Without socketio, but this does not change the essence. The author saves all clients in an array and works with this array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question