Socket IO doesn't get emitted from client - Node JS
I am working on Socket IO, the connection between the client and the server is established successfully. I am facing two problems:
1 - When the initial connection is made between the client and the server, the socket.client.id
on server and socket.id
on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue / problem while communicating with the server or even with the client using sockets, while not having the same ids ? or does the id on the server get changed when the client page is refreshed ?
2 - On the initial connection establishment the socket passes a messages, using socket.emit()
from server and receives as socket.on()
on client. But when I try to emit anything from client it doesn't get received on server.
Socket Connections
function Globals()
this.socketConnection = async function()
let p = new Promise(function(res, rej)
io.on("connection", function(socket)
if (socket.connected)
res(socket);
else
rej("Socket Connection Error !");
)
)
return await p;
new Globals().socketConnection().then(function(soc)
console.log(soc.client.id);
socket = soc;
soc.emit("Hi");
soc.on("Nady", function()
console.log("I am called");
)
)
Client Side Connection
function Globals()
this.socketConnection = async function()
var socket = io('http://localhost:8080');
let p = new Promise(function(res, rej)
socket.on('connect', function()
if (socket.connected)
console.log(socket.id);
res(socket);
)
)
return await p;
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
function ScrapJobs()
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
var _this = this;
this.attachListeners = function()
qs("#init-scrap").ev("click", _this.startScrapping);
this.startScrapping = function()
console.log("I am cliced");
socket.on("Hi", function()
console.log("Hi Nadeem");
)
socket.emit("Nady");
node.js sockets socket.io
add a comment |
I am working on Socket IO, the connection between the client and the server is established successfully. I am facing two problems:
1 - When the initial connection is made between the client and the server, the socket.client.id
on server and socket.id
on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue / problem while communicating with the server or even with the client using sockets, while not having the same ids ? or does the id on the server get changed when the client page is refreshed ?
2 - On the initial connection establishment the socket passes a messages, using socket.emit()
from server and receives as socket.on()
on client. But when I try to emit anything from client it doesn't get received on server.
Socket Connections
function Globals()
this.socketConnection = async function()
let p = new Promise(function(res, rej)
io.on("connection", function(socket)
if (socket.connected)
res(socket);
else
rej("Socket Connection Error !");
)
)
return await p;
new Globals().socketConnection().then(function(soc)
console.log(soc.client.id);
socket = soc;
soc.emit("Hi");
soc.on("Nady", function()
console.log("I am called");
)
)
Client Side Connection
function Globals()
this.socketConnection = async function()
var socket = io('http://localhost:8080');
let p = new Promise(function(res, rej)
socket.on('connect', function()
if (socket.connected)
console.log(socket.id);
res(socket);
)
)
return await p;
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
function ScrapJobs()
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
var _this = this;
this.attachListeners = function()
qs("#init-scrap").ev("click", _this.startScrapping);
this.startScrapping = function()
console.log("I am cliced");
socket.on("Hi", function()
console.log("Hi Nadeem");
)
socket.emit("Nady");
node.js sockets socket.io
add a comment |
I am working on Socket IO, the connection between the client and the server is established successfully. I am facing two problems:
1 - When the initial connection is made between the client and the server, the socket.client.id
on server and socket.id
on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue / problem while communicating with the server or even with the client using sockets, while not having the same ids ? or does the id on the server get changed when the client page is refreshed ?
2 - On the initial connection establishment the socket passes a messages, using socket.emit()
from server and receives as socket.on()
on client. But when I try to emit anything from client it doesn't get received on server.
Socket Connections
function Globals()
this.socketConnection = async function()
let p = new Promise(function(res, rej)
io.on("connection", function(socket)
if (socket.connected)
res(socket);
else
rej("Socket Connection Error !");
)
)
return await p;
new Globals().socketConnection().then(function(soc)
console.log(soc.client.id);
socket = soc;
soc.emit("Hi");
soc.on("Nady", function()
console.log("I am called");
)
)
Client Side Connection
function Globals()
this.socketConnection = async function()
var socket = io('http://localhost:8080');
let p = new Promise(function(res, rej)
socket.on('connect', function()
if (socket.connected)
console.log(socket.id);
res(socket);
)
)
return await p;
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
function ScrapJobs()
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
var _this = this;
this.attachListeners = function()
qs("#init-scrap").ev("click", _this.startScrapping);
this.startScrapping = function()
console.log("I am cliced");
socket.on("Hi", function()
console.log("Hi Nadeem");
)
socket.emit("Nady");
node.js sockets socket.io
I am working on Socket IO, the connection between the client and the server is established successfully. I am facing two problems:
1 - When the initial connection is made between the client and the server, the socket.client.id
on server and socket.id
on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue / problem while communicating with the server or even with the client using sockets, while not having the same ids ? or does the id on the server get changed when the client page is refreshed ?
2 - On the initial connection establishment the socket passes a messages, using socket.emit()
from server and receives as socket.on()
on client. But when I try to emit anything from client it doesn't get received on server.
Socket Connections
function Globals()
this.socketConnection = async function()
let p = new Promise(function(res, rej)
io.on("connection", function(socket)
if (socket.connected)
res(socket);
else
rej("Socket Connection Error !");
)
)
return await p;
new Globals().socketConnection().then(function(soc)
console.log(soc.client.id);
socket = soc;
soc.emit("Hi");
soc.on("Nady", function()
console.log("I am called");
)
)
Client Side Connection
function Globals()
this.socketConnection = async function()
var socket = io('http://localhost:8080');
let p = new Promise(function(res, rej)
socket.on('connect', function()
if (socket.connected)
console.log(socket.id);
res(socket);
)
)
return await p;
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
function ScrapJobs()
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
var _this = this;
this.attachListeners = function()
qs("#init-scrap").ev("click", _this.startScrapping);
this.startScrapping = function()
console.log("I am cliced");
socket.on("Hi", function()
console.log("Hi Nadeem");
)
socket.emit("Nady");
function Globals()
this.socketConnection = async function()
let p = new Promise(function(res, rej)
io.on("connection", function(socket)
if (socket.connected)
res(socket);
else
rej("Socket Connection Error !");
)
)
return await p;
new Globals().socketConnection().then(function(soc)
console.log(soc.client.id);
socket = soc;
soc.emit("Hi");
soc.on("Nady", function()
console.log("I am called");
)
)
function Globals()
this.socketConnection = async function()
let p = new Promise(function(res, rej)
io.on("connection", function(socket)
if (socket.connected)
res(socket);
else
rej("Socket Connection Error !");
)
)
return await p;
new Globals().socketConnection().then(function(soc)
console.log(soc.client.id);
socket = soc;
soc.emit("Hi");
soc.on("Nady", function()
console.log("I am called");
)
)
function Globals()
this.socketConnection = async function()
var socket = io('http://localhost:8080');
let p = new Promise(function(res, rej)
socket.on('connect', function()
if (socket.connected)
console.log(socket.id);
res(socket);
)
)
return await p;
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
function ScrapJobs()
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
var _this = this;
this.attachListeners = function()
qs("#init-scrap").ev("click", _this.startScrapping);
this.startScrapping = function()
console.log("I am cliced");
socket.on("Hi", function()
console.log("Hi Nadeem");
)
socket.emit("Nady");
function Globals()
this.socketConnection = async function()
var socket = io('http://localhost:8080');
let p = new Promise(function(res, rej)
socket.on('connect', function()
if (socket.connected)
console.log(socket.id);
res(socket);
)
)
return await p;
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
function ScrapJobs()
var socket;
new App().socketConnection().then(function(s)
socket = s;
);
var _this = this;
this.attachListeners = function()
qs("#init-scrap").ev("click", _this.startScrapping);
this.startScrapping = function()
console.log("I am cliced");
socket.on("Hi", function()
console.log("Hi Nadeem");
)
socket.emit("Nady");
node.js sockets socket.io
node.js sockets socket.io
asked Nov 13 '18 at 8:07
Nadeem AhmadNadeem Ahmad
838
838
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue
The client side socket.id
value is set on the client socket object after the connect
event is received and is updated (e.g. modified) upon a reconnect
event.
It appears that the socket.io infrastructure will keep them the same on client and server. If the client disconnects and then reconnects, there will be a new connection with a new id on both client and server. It is possible you are attempting to hang onto the old socket object on the server after the client has disconnected it (we can't really see enough of your server code to evaluate that).
On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.
You'd have to show us a reproducible case. This does not happen if you are coding things correctly. I would guess that you do not have the right listeners for messages on the right socket in order to see the messages you are sending. I promise you that sending a message from client to server works just fine when implemented properly.
A general comment about your code. Both code blocks you show appear to be stuffing a socket
object into a higher scoped (or perhaps even global) variable. That is likely part the cause of your problem because that socket object can become dead if the client reconnects for any reason. Plus putting any sort of socket object into a global or module level variable makes your server only capable of serving one client - it's simply not how you design multi-client servers.
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
1
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276437%2fsocket-io-doesnt-get-emitted-from-client-node-js%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
When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue
The client side socket.id
value is set on the client socket object after the connect
event is received and is updated (e.g. modified) upon a reconnect
event.
It appears that the socket.io infrastructure will keep them the same on client and server. If the client disconnects and then reconnects, there will be a new connection with a new id on both client and server. It is possible you are attempting to hang onto the old socket object on the server after the client has disconnected it (we can't really see enough of your server code to evaluate that).
On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.
You'd have to show us a reproducible case. This does not happen if you are coding things correctly. I would guess that you do not have the right listeners for messages on the right socket in order to see the messages you are sending. I promise you that sending a message from client to server works just fine when implemented properly.
A general comment about your code. Both code blocks you show appear to be stuffing a socket
object into a higher scoped (or perhaps even global) variable. That is likely part the cause of your problem because that socket object can become dead if the client reconnects for any reason. Plus putting any sort of socket object into a global or module level variable makes your server only capable of serving one client - it's simply not how you design multi-client servers.
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
1
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
add a comment |
When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue
The client side socket.id
value is set on the client socket object after the connect
event is received and is updated (e.g. modified) upon a reconnect
event.
It appears that the socket.io infrastructure will keep them the same on client and server. If the client disconnects and then reconnects, there will be a new connection with a new id on both client and server. It is possible you are attempting to hang onto the old socket object on the server after the client has disconnected it (we can't really see enough of your server code to evaluate that).
On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.
You'd have to show us a reproducible case. This does not happen if you are coding things correctly. I would guess that you do not have the right listeners for messages on the right socket in order to see the messages you are sending. I promise you that sending a message from client to server works just fine when implemented properly.
A general comment about your code. Both code blocks you show appear to be stuffing a socket
object into a higher scoped (or perhaps even global) variable. That is likely part the cause of your problem because that socket object can become dead if the client reconnects for any reason. Plus putting any sort of socket object into a global or module level variable makes your server only capable of serving one client - it's simply not how you design multi-client servers.
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
1
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
add a comment |
When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue
The client side socket.id
value is set on the client socket object after the connect
event is received and is updated (e.g. modified) upon a reconnect
event.
It appears that the socket.io infrastructure will keep them the same on client and server. If the client disconnects and then reconnects, there will be a new connection with a new id on both client and server. It is possible you are attempting to hang onto the old socket object on the server after the client has disconnected it (we can't really see enough of your server code to evaluate that).
On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.
You'd have to show us a reproducible case. This does not happen if you are coding things correctly. I would guess that you do not have the right listeners for messages on the right socket in order to see the messages you are sending. I promise you that sending a message from client to server works just fine when implemented properly.
A general comment about your code. Both code blocks you show appear to be stuffing a socket
object into a higher scoped (or perhaps even global) variable. That is likely part the cause of your problem because that socket object can become dead if the client reconnects for any reason. Plus putting any sort of socket object into a global or module level variable makes your server only capable of serving one client - it's simply not how you design multi-client servers.
When the initial connection is made between the client and the server, the socket.client.id on server and socket.id on client side, both are the same, but when I refresh the client page, the id of the client changes to other one, but on the server it is still the same. Does it makes any issue
The client side socket.id
value is set on the client socket object after the connect
event is received and is updated (e.g. modified) upon a reconnect
event.
It appears that the socket.io infrastructure will keep them the same on client and server. If the client disconnects and then reconnects, there will be a new connection with a new id on both client and server. It is possible you are attempting to hang onto the old socket object on the server after the client has disconnected it (we can't really see enough of your server code to evaluate that).
On the initial connection establishment the socket passes a messages, using socket.emit() from server and receives as socket.on() on client. But when I try to emit anything from client it doesn't get received on server.
You'd have to show us a reproducible case. This does not happen if you are coding things correctly. I would guess that you do not have the right listeners for messages on the right socket in order to see the messages you are sending. I promise you that sending a message from client to server works just fine when implemented properly.
A general comment about your code. Both code blocks you show appear to be stuffing a socket
object into a higher scoped (or perhaps even global) variable. That is likely part the cause of your problem because that socket object can become dead if the client reconnects for any reason. Plus putting any sort of socket object into a global or module level variable makes your server only capable of serving one client - it's simply not how you design multi-client servers.
answered Nov 13 '18 at 8:38
jfriend00jfriend00
429k54550599
429k54550599
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
1
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
add a comment |
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
1
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
the second problem has been resolved, and I made the socket connection available to each Constructor function. About the first one, is it good experience to stick to old socket object, rather then reconnecting and getting a new socket object ?
– Nadeem Ahmad
Nov 13 '18 at 10:00
1
1
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
@NadeemAhmad - Socket objects are good as long as they are connected. There is no reason to manually disconnect and reconnect. In fact a feature of socket.io is that it automatically pings the other end and will automatically disconnect and reconnect if the connection goes silent for any reason. You do not have to do that yourself.
– jfriend00
Nov 13 '18 at 22:20
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
Perfect, I got you :)
– Nadeem Ahmad
Nov 14 '18 at 7:17
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276437%2fsocket-io-doesnt-get-emitted-from-client-node-js%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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