So currently my setup is I have a standard app.get('/', etc for my index and inside here, I have my io.on('connection', function etc). Now the goal is so that every time someone connects to only the homepage i can get that socket with io.on(connection and send things to it that way, and my syntax and all is fine however i believe having my io.on('connection' inside a route is my issue.
The problem: Whenever someone connects to my website after i start the server, it works great, for debug examples i have a console.log inside of it and its called once and we are good. However if that same person reloads the page my io.on('connection' is called again, and this time iw ill get 2 console.log's... when I again reload I then get 3 and so on and so on, no matter if i close the page and reopen it or come from a different ip. It seems as if the connection isnt closed when I reload and all the still hanging connections are recalled when I reload.
I know this is a little unorthodox with me not posting my code. Its not the syntax, here is an example of essentially the set up described. Oh and also i need access to the req input from the app.get which is why its in there in the first place, I have passport variables saved in it.
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
io.on('connection', function(socket){
console.log("1 connection");
});
});
I hope this explains my issue well enough. When i looked for answers first i found a bunch of stuff about routing, but was confused. Any help is appreciated!
For what I got from the question and the comments.
You are doing it wrong way. You should never put the ONs and EMITs of socket connection where they are called multiple times as they are appended every time.
for example : first time this is called
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
io.on('connection', function(socket){
console.log("1 connection");
});
});
the io.ons['connection']
has the value
function(socket){
console.log("1 connection");
}
second time you call it the callback is appended again. and the value of io.ons['connection']
is now
function(socket){
console.log("1 connection");
}
function(socket){
console.log("1 connection");
}
So it prints console.log two times
SECOND :
if you want to do the further work after the user is logged in.then you need to verify the user here, you can use a socket.emit('init',.....
from client side
and server-side socket.on('init',......
will verify the user and can access else return and close the connection.
Never, ever put event handlers like io.on()
inside app.get()
handlers. That is just wrong and will not do even close to what you want. It will not have an event handler in place until someone hits your page and then every time someone hits that page, it will add a duplicate event handler. Both of these are wrong.
The structure should look like this:
// watch for people hitting the / route in the browser
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// listen for incoming webSocket connections
io.on('connection', function(socket){
console.log("new connection");
});
This will prevent the duplicate event handlers you were getting.
In case what you were trying to do is to listen for incoming socket.io connections only from a specific page, that is not a support capability. Socket.io connections are from a browser, not from a specific route. You can get access to the cookies associated with the hosting page and the code in the web page making the browser can connect to something like a specific namespace, but there is no built in correlation between an incoming socket.io connection and a specific route the way there is with http requests.
If you're trying to get access to a session object from an incoming socket.io connection, that can usually be done via the cookies associated with the start of the socket.io connection.