How can I get a referent to connect socket.io

I have a simple node.js web server and I am using socket.io.

Users can perform simple tasks on a server, which is a single-page application running on / . If the user has an existing state on the server, the user ID can be added to the URL, for example. /{my-id} .

I need to extract id from url when establishing socket.io connection. Is it possible to get the URL for the client page from which the socket is created; connection request referrer?

+5
source share
1 answer

Using console.log I was able to find this value as follows:

 io.on('connection', function (socket) { var referer = socket.request.headers.referer; } 

It works, but since it is not documented, I do not know if this is recommended, or if it will change.

+7
source

Source: https://habr.com/ru/post/1203421/


All Articles