How to assign a unique identifier to a Websocket












0















Using socket.ioin a Node/Express environment, is it possible to uniquely assign, say someone's login id, when a socket connection is made?



Updated



Within my app.js (express) file, I have the following code to access the login, from the request headers:



const express = require('express');
const app = express();

app.use(function(req, res, next){
let login = req.header('login');
next();
});


I need a means of being able to broadcast messages using this unique identifier (login) via socket.io










share|improve this question

























  • It's not hard to assign a property to a webSocket object. It's not hard to keep a Map object with all the currently webSocket objects indexed by userID they belong to. What we don't know from your environment is how you associate the right userID with a new incoming webSocket connection. That you'd have to show us and then I could show you the rest of the code as a complete set.

    – jfriend00
    Nov 14 '18 at 23:58











  • @jfriend00 Thanks for the reply. I'm new to websockets and I guess what you are asking me is basically what I am trying to achieve.My thinking is that when a "io.on('connection', (socket) => {" call is made, I have the login id when the user fires up the browser app. Unsure if I am missing something.

    – tonyf
    Nov 15 '18 at 0:31











  • Are you using socket.io or plain webSocket (you have indications of both in your question and socket.io would make things easier)? Does your user already authenticate in your web page and use a cookie to maintain that logged in state?

    – jfriend00
    Nov 15 '18 at 0:48













  • @jfriend00 Apologies. I am using socket.io and have updated my original post. Correct on the other question you asked - yes, user is already authenticated and cookie use. Hope this helps.

    – tonyf
    Nov 15 '18 at 0:56
















0















Using socket.ioin a Node/Express environment, is it possible to uniquely assign, say someone's login id, when a socket connection is made?



Updated



Within my app.js (express) file, I have the following code to access the login, from the request headers:



const express = require('express');
const app = express();

app.use(function(req, res, next){
let login = req.header('login');
next();
});


I need a means of being able to broadcast messages using this unique identifier (login) via socket.io










share|improve this question

























  • It's not hard to assign a property to a webSocket object. It's not hard to keep a Map object with all the currently webSocket objects indexed by userID they belong to. What we don't know from your environment is how you associate the right userID with a new incoming webSocket connection. That you'd have to show us and then I could show you the rest of the code as a complete set.

    – jfriend00
    Nov 14 '18 at 23:58











  • @jfriend00 Thanks for the reply. I'm new to websockets and I guess what you are asking me is basically what I am trying to achieve.My thinking is that when a "io.on('connection', (socket) => {" call is made, I have the login id when the user fires up the browser app. Unsure if I am missing something.

    – tonyf
    Nov 15 '18 at 0:31











  • Are you using socket.io or plain webSocket (you have indications of both in your question and socket.io would make things easier)? Does your user already authenticate in your web page and use a cookie to maintain that logged in state?

    – jfriend00
    Nov 15 '18 at 0:48













  • @jfriend00 Apologies. I am using socket.io and have updated my original post. Correct on the other question you asked - yes, user is already authenticated and cookie use. Hope this helps.

    – tonyf
    Nov 15 '18 at 0:56














0












0








0








Using socket.ioin a Node/Express environment, is it possible to uniquely assign, say someone's login id, when a socket connection is made?



Updated



Within my app.js (express) file, I have the following code to access the login, from the request headers:



const express = require('express');
const app = express();

app.use(function(req, res, next){
let login = req.header('login');
next();
});


I need a means of being able to broadcast messages using this unique identifier (login) via socket.io










share|improve this question
















Using socket.ioin a Node/Express environment, is it possible to uniquely assign, say someone's login id, when a socket connection is made?



Updated



Within my app.js (express) file, I have the following code to access the login, from the request headers:



const express = require('express');
const app = express();

app.use(function(req, res, next){
let login = req.header('login');
next();
});


I need a means of being able to broadcast messages using this unique identifier (login) via socket.io







node.js express socket.io






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:11









halfer

14.6k758112




14.6k758112










asked Nov 14 '18 at 23:08









tonyftonyf

12.6k37114190




12.6k37114190













  • It's not hard to assign a property to a webSocket object. It's not hard to keep a Map object with all the currently webSocket objects indexed by userID they belong to. What we don't know from your environment is how you associate the right userID with a new incoming webSocket connection. That you'd have to show us and then I could show you the rest of the code as a complete set.

    – jfriend00
    Nov 14 '18 at 23:58











  • @jfriend00 Thanks for the reply. I'm new to websockets and I guess what you are asking me is basically what I am trying to achieve.My thinking is that when a "io.on('connection', (socket) => {" call is made, I have the login id when the user fires up the browser app. Unsure if I am missing something.

    – tonyf
    Nov 15 '18 at 0:31











  • Are you using socket.io or plain webSocket (you have indications of both in your question and socket.io would make things easier)? Does your user already authenticate in your web page and use a cookie to maintain that logged in state?

    – jfriend00
    Nov 15 '18 at 0:48













  • @jfriend00 Apologies. I am using socket.io and have updated my original post. Correct on the other question you asked - yes, user is already authenticated and cookie use. Hope this helps.

    – tonyf
    Nov 15 '18 at 0:56



















  • It's not hard to assign a property to a webSocket object. It's not hard to keep a Map object with all the currently webSocket objects indexed by userID they belong to. What we don't know from your environment is how you associate the right userID with a new incoming webSocket connection. That you'd have to show us and then I could show you the rest of the code as a complete set.

    – jfriend00
    Nov 14 '18 at 23:58











  • @jfriend00 Thanks for the reply. I'm new to websockets and I guess what you are asking me is basically what I am trying to achieve.My thinking is that when a "io.on('connection', (socket) => {" call is made, I have the login id when the user fires up the browser app. Unsure if I am missing something.

    – tonyf
    Nov 15 '18 at 0:31











  • Are you using socket.io or plain webSocket (you have indications of both in your question and socket.io would make things easier)? Does your user already authenticate in your web page and use a cookie to maintain that logged in state?

    – jfriend00
    Nov 15 '18 at 0:48













  • @jfriend00 Apologies. I am using socket.io and have updated my original post. Correct on the other question you asked - yes, user is already authenticated and cookie use. Hope this helps.

    – tonyf
    Nov 15 '18 at 0:56

















It's not hard to assign a property to a webSocket object. It's not hard to keep a Map object with all the currently webSocket objects indexed by userID they belong to. What we don't know from your environment is how you associate the right userID with a new incoming webSocket connection. That you'd have to show us and then I could show you the rest of the code as a complete set.

– jfriend00
Nov 14 '18 at 23:58





It's not hard to assign a property to a webSocket object. It's not hard to keep a Map object with all the currently webSocket objects indexed by userID they belong to. What we don't know from your environment is how you associate the right userID with a new incoming webSocket connection. That you'd have to show us and then I could show you the rest of the code as a complete set.

– jfriend00
Nov 14 '18 at 23:58













@jfriend00 Thanks for the reply. I'm new to websockets and I guess what you are asking me is basically what I am trying to achieve.My thinking is that when a "io.on('connection', (socket) => {" call is made, I have the login id when the user fires up the browser app. Unsure if I am missing something.

– tonyf
Nov 15 '18 at 0:31





@jfriend00 Thanks for the reply. I'm new to websockets and I guess what you are asking me is basically what I am trying to achieve.My thinking is that when a "io.on('connection', (socket) => {" call is made, I have the login id when the user fires up the browser app. Unsure if I am missing something.

– tonyf
Nov 15 '18 at 0:31













Are you using socket.io or plain webSocket (you have indications of both in your question and socket.io would make things easier)? Does your user already authenticate in your web page and use a cookie to maintain that logged in state?

– jfriend00
Nov 15 '18 at 0:48







Are you using socket.io or plain webSocket (you have indications of both in your question and socket.io would make things easier)? Does your user already authenticate in your web page and use a cookie to maintain that logged in state?

– jfriend00
Nov 15 '18 at 0:48















@jfriend00 Apologies. I am using socket.io and have updated my original post. Correct on the other question you asked - yes, user is already authenticated and cookie use. Hope this helps.

– tonyf
Nov 15 '18 at 0:56





@jfriend00 Apologies. I am using socket.io and have updated my original post. Correct on the other question you asked - yes, user is already authenticated and cookie use. Hope this helps.

– tonyf
Nov 15 '18 at 0:56












1 Answer
1






active

oldest

votes


















1














OK, I'll assume that you have the ability to figure out which user it is from the login cookie. Since you don't show us how you set up the cookie, you will have to write your own code to do that.



When a socket.io connection event occurs, you can get to the cookies from the initial socket.io connection request in socket.request.headers.cookie. Let's assume you have a function named getUserFromCookie() that you pass a login cookie to and it returns the userID and we'll assume your login cookie is named "login". Then, you could write code like this:



io.on('connection', socket => {
let userId = getUserFromCookie(socket.request.headers.cookie);
// join user to a room with the name of their userId
socket.join(userId);
});


Then, elsewhere in your server code where you want to send a message to a particular userId, you can do it by just doing this:



io.to(userId).emit("someMsg", someData);





share|improve this answer
























  • With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

    – tonyf
    Nov 19 '18 at 6:05











  • @tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

    – jfriend00
    Nov 19 '18 at 6:15











  • instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

    – tonyf
    Nov 19 '18 at 6:21











  • @tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

    – jfriend00
    Nov 19 '18 at 6:23













  • I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

    – tonyf
    Nov 19 '18 at 6:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310106%2fhow-to-assign-a-unique-identifier-to-a-websocket%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














OK, I'll assume that you have the ability to figure out which user it is from the login cookie. Since you don't show us how you set up the cookie, you will have to write your own code to do that.



When a socket.io connection event occurs, you can get to the cookies from the initial socket.io connection request in socket.request.headers.cookie. Let's assume you have a function named getUserFromCookie() that you pass a login cookie to and it returns the userID and we'll assume your login cookie is named "login". Then, you could write code like this:



io.on('connection', socket => {
let userId = getUserFromCookie(socket.request.headers.cookie);
// join user to a room with the name of their userId
socket.join(userId);
});


Then, elsewhere in your server code where you want to send a message to a particular userId, you can do it by just doing this:



io.to(userId).emit("someMsg", someData);





share|improve this answer
























  • With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

    – tonyf
    Nov 19 '18 at 6:05











  • @tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

    – jfriend00
    Nov 19 '18 at 6:15











  • instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

    – tonyf
    Nov 19 '18 at 6:21











  • @tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

    – jfriend00
    Nov 19 '18 at 6:23













  • I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

    – tonyf
    Nov 19 '18 at 6:28
















1














OK, I'll assume that you have the ability to figure out which user it is from the login cookie. Since you don't show us how you set up the cookie, you will have to write your own code to do that.



When a socket.io connection event occurs, you can get to the cookies from the initial socket.io connection request in socket.request.headers.cookie. Let's assume you have a function named getUserFromCookie() that you pass a login cookie to and it returns the userID and we'll assume your login cookie is named "login". Then, you could write code like this:



io.on('connection', socket => {
let userId = getUserFromCookie(socket.request.headers.cookie);
// join user to a room with the name of their userId
socket.join(userId);
});


Then, elsewhere in your server code where you want to send a message to a particular userId, you can do it by just doing this:



io.to(userId).emit("someMsg", someData);





share|improve this answer
























  • With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

    – tonyf
    Nov 19 '18 at 6:05











  • @tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

    – jfriend00
    Nov 19 '18 at 6:15











  • instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

    – tonyf
    Nov 19 '18 at 6:21











  • @tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

    – jfriend00
    Nov 19 '18 at 6:23













  • I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

    – tonyf
    Nov 19 '18 at 6:28














1












1








1







OK, I'll assume that you have the ability to figure out which user it is from the login cookie. Since you don't show us how you set up the cookie, you will have to write your own code to do that.



When a socket.io connection event occurs, you can get to the cookies from the initial socket.io connection request in socket.request.headers.cookie. Let's assume you have a function named getUserFromCookie() that you pass a login cookie to and it returns the userID and we'll assume your login cookie is named "login". Then, you could write code like this:



io.on('connection', socket => {
let userId = getUserFromCookie(socket.request.headers.cookie);
// join user to a room with the name of their userId
socket.join(userId);
});


Then, elsewhere in your server code where you want to send a message to a particular userId, you can do it by just doing this:



io.to(userId).emit("someMsg", someData);





share|improve this answer













OK, I'll assume that you have the ability to figure out which user it is from the login cookie. Since you don't show us how you set up the cookie, you will have to write your own code to do that.



When a socket.io connection event occurs, you can get to the cookies from the initial socket.io connection request in socket.request.headers.cookie. Let's assume you have a function named getUserFromCookie() that you pass a login cookie to and it returns the userID and we'll assume your login cookie is named "login". Then, you could write code like this:



io.on('connection', socket => {
let userId = getUserFromCookie(socket.request.headers.cookie);
// join user to a room with the name of their userId
socket.join(userId);
});


Then, elsewhere in your server code where you want to send a message to a particular userId, you can do it by just doing this:



io.to(userId).emit("someMsg", someData);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 2:39









jfriend00jfriend00

435k55567615




435k55567615













  • With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

    – tonyf
    Nov 19 '18 at 6:05











  • @tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

    – jfriend00
    Nov 19 '18 at 6:15











  • instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

    – tonyf
    Nov 19 '18 at 6:21











  • @tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

    – jfriend00
    Nov 19 '18 at 6:23













  • I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

    – tonyf
    Nov 19 '18 at 6:28



















  • With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

    – tonyf
    Nov 19 '18 at 6:05











  • @tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

    – jfriend00
    Nov 19 '18 at 6:15











  • instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

    – tonyf
    Nov 19 '18 at 6:21











  • @tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

    – jfriend00
    Nov 19 '18 at 6:23













  • I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

    – tonyf
    Nov 19 '18 at 6:28

















With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

– tonyf
Nov 19 '18 at 6:05





With the user id, I actually have this set using sessionStorage and apologies, it's actually not using cookies to store the logged in user. With the means that you have suggested above, to obtain the user, the user id value is available as part of the Request Headers. Is there a means of obtaining this value using socket.io?

– tonyf
Nov 19 '18 at 6:05













@tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

– jfriend00
Nov 19 '18 at 6:15





@tonyf - You can get access to your session from socket.io. You'd have to show more about your session code to know what to suggest.

– jfriend00
Nov 19 '18 at 6:15













instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

– tonyf
Nov 19 '18 at 6:21





instead of your let user code, it would read let userId = sessionStorage.getItem('login'); BTW, just to confirm, is your io.on connection code on the server or client - I'm assuming it's on the server - correct?

– tonyf
Nov 19 '18 at 6:21













@tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

– jfriend00
Nov 19 '18 at 6:23







@tonyf - Yes, this code is on the server. Yes, you would get the userID from your session object. You will need to work out how to get the session object from inside of socket.io. If you're using express-session for your session management, you can probably use something like this express-session-socket.io.

– jfriend00
Nov 19 '18 at 6:23















I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

– tonyf
Nov 19 '18 at 6:28





I just had a quick look at your express-session link but unsure how to access the session storage as the userid is set at startup within an angular 5 js file. Will investigate further.

– tonyf
Nov 19 '18 at 6:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310106%2fhow-to-assign-a-unique-identifier-to-a-websocket%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly