Socket.IO is not triggering the connect event - node.js

I'm trying to setup a Socket.IO server, and right now, connecting doesn't seem to be working the way it does in the Wiki. I'm connecting to a server using the namespace /client, and I'm successfully seeing the connection made in the debug log, but the connection message is never display (and the other contents are never getting attached).
Server-side
var clients = io
.of('/client')
.on('connect', function (socket) {
// This is never getting run
console.log('Client connected');
});
Client side
var socket = io.connect('http://localhost:8082/client');
Why, in the above code, am I not getting the 'Client connected' message in my console?

So, turns out this is a very simple issue, as I suspected. The client uses the connect event, but the server uses connection.
The code should look like:
var clients = io
.of('/client')
.on('connection', function (socket) {
// This is never getting run
console.log('Client connected');
});

Related

Can't receive data from io.emit()

I am trying to send data to all clients just to test how socket.io works.
I can't seem to get any data back from the server, no matter how I try.
The event is fired, so the data gets to the server side, but it doesn't arrive to the clients.
On the server side, I do this:
io.on('connection', (socket) => {
socket.on('chatMessage', data => {
console.log(data);
io.emit(data);
});
});
The console.log() here works, so its fine until that line.
On the client side I tried to simplify things to see the result like this:
const socket = io.connect('http://localhost:5000');
socket.on('connect', () => {
socket.emit('chatMessage', {a:"fsdfsdfsd"});
});
socket.on('chatMessage', data =>{
console.log(data);
});
The socket.emit() fires, but it doesn't arrive anywhere.
I have never used socket.io before, but I read the docs and it said io.emit() sends the data to all connected clients.
Just in case, I tried with multiple clients connected, but it still doesn't work.
First thing I would recommend is to check if the client is really connecting to the server socket by adding a console.log() inside your on connection block in the server side.
Also, your io.emit(data); in the server side won't work since the first parameter of the emit function should be the event name.

socket.io - .on not working on client side

I have some very basic code that uses socket.io
Client
var socket = io();
// Do some stuff...
socket.emit('hello', 'please like me');
// Wait for a response
socket.on('hello back!', function(msg) {
console.log('Yay, he replied:', msg);
});
Server
var app = require('express');
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('disconnect', function() {
console.log('user disconnected');
});
socket.on('hello', function() {
// Code to decide wether or not I like the client
// ...
// I do like the client!
socket.emit('hello back!', 'how are you');
console.log('I said hello back!');
});
});
Hopefully you managed to get the picture. So ideally, this would happen:
Client connects
After a while, client emits hello
Server sees this, emits hello back!
Client notices the hello back! event
Everyone is happy!
But this isn't the case for me unfortunately. Instead, steps 1-3 work perfectly. But when you reach step 4 and 5, things start to fall apart. Client never logs anything after hello back! event is emitted.
This is not my full code, I figured it would make it easier for you to understand it. It's very possible that I've made some silly mistake somewhere, maybe without including it in this code. But please let me know if there is anything fundamentally wrong with what I am doing.
Cheers
This is a few years old, but in case anyone stumbles across this in the future:
My problem had nothing to do with my code shown above. Instead, it was a configuration error.
I was hosting my project on glitch.com who state that they support websockets, but the error was caused because the server was not able to get across to the client (client never received any data from server at all after websocket connection was opened).
This was an error that I had in 2018 with glitch.com, and I know that other people have had it too. It also seemed to be related somehow to websockets connecting via a domain name and with SSL, but I can't recall the exact circumstances.

socket.io client side in nodejs app

i am using socket.io for communication between server and client.
My client side is not a html. My client side is javascript file. so here is my code of client side
var io = require('socket.io-client')
var socket = io.connect('http://localhost:3000/home');
socket.on('connect', function () {
console.log(' Connected!');
});
On server side i have received the connection event but on client side connect event doesn't fire. I have tested through html way, it works but why its not working through a java script file.
I have no idea what you mean with tested through html way, but try socket.on("connection", ...),
not "connect".
could be related to the fact that you're specifying a namespace different from the default.
try to handle the connection at http://localhost:3000, instead of http://localhost:3000/home
var socket = io.connect('http://localhost:3000');
found a similar situation here

Socket.io fires connect event just after the second connect

I'm using socket.io with the latest version of node.js and socket.io shows an curious behavior:
The connect event fires on client side but not on server side for the first time.
After a reload or simply loading the page again the event got fired on both - client and server - side correctly.
What's wrong (with my code?)?
// Client
var socket = io.connect(window.location.protocol + '//' + window.location.hostname, {
'sync disconnect on unload': true
});
socket.on('connect', function() {
alert("Connect");
// Do other stuff
});
-
// Server
io.sockets.on('connection', function(socket) {
console.log("connection");
io.sockets.on('connect', function(socket) {
console.log("connect");
});
});
Started the server and loaded the page:
Client fires the alert, server just logs connection. Now loading the page again, it logs both connection and connect.
Update:
It seems that just the very first connection has such issues, afterwards it works everywhere as expected. Just after every node server (re)start, that behavior appears.
Note: node itself delivers the page where the socket.io is used and that works even on the first request, so a node issue should be excluded then.
Browser is also doesn't matter, it's the same on every browser.
For Socket.IO, connection is the server-side equivalent of connect on the client side. Therefore, when you're inside the callback for the connection event, the socket has already established a connection and you don't need to listen on some other event. I'm not sure what connect on the server side is, but it is not documented and does not work.
// on the server
io.sockets.on('connection', function(socket) {
// this connection has already been established
// no other handler is required
});
// on the client
var socket = io.connect();
socket.on('connect', function() {
// the connection has been established
});

emitting data via socket on browser close /window close

I need to send data to nodejs server via socket.io when the user closes the browser tab .
I tried doing :
var data={};
window.onbeforeunload = function() {
// i have a object to be sent
data.data1='abcd';
data.data2=1234;
socket.emit("senddata",data);
}
This code works when the user navigates around clicking links on the site but doesnot work when the user closes the browser tab
I also tried configuring the socket io on server side as below .. thinking the error may be due to socket connection being closed before emitting data:
var io = require('socket.io').listen(app.listen(port));
io.configure(function () {
io.set('close timeout',12000);
});
It also didnt work most of the time.
I also tried this on client side:
var socket = require('socket.io').listen(80, {
"sync disconnect on unload":false
});
It also did not work
I had tried receiving data like this
var io = require('socket.io').listen(app.listen(port));
io.sockets.on('connection', function (socket) {
socket.on('senddata', function (data) {
// data processing
});
});
please ..help me with this problem..thanks in advance..
When user connects - register on server side the time it happened.
Socket.IO has heart beating and pinging functionality. So just subscribe to disconnect event on server side and once it happens - means client disconnected - and you have on server time when client had connection. So that way you have timespan of client session.
Do not trust client to tell you any time or important data, as client can simply 'lie' - which leads to hacks/cheats/bugs on your server-side.
There is no reliable way to send data just before disconnect from client-side at all. There is nothing in Socket.IO for that, nor in just one of transports (WebSockets). As well as there is too many different scenarios of disconnection (power off, force close, ethernet cable out, wifi lose, etc).

Resources