socket.broadcast.emit doesn't fire correctly
up vote
0
down vote
favorite
Node create a unique url and bind socket.io to it.
var io = require("socket.io").listen(server, {path: req.originalUrl});
When a client connects also binds his socket.io-client to that url
var socket = io('192.168.1.101:3000', {path: window.location.pathname});
I don't have problems and everything works great.
When a client performs a particular action, server do
socket.broadcast.emit("foo"); //I made console.log here and it prints
client-side:
socket.on("foo", () => console.log("okay"));
The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.
I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
error
node.js websocket socket.io
add a comment |
up vote
0
down vote
favorite
Node create a unique url and bind socket.io to it.
var io = require("socket.io").listen(server, {path: req.originalUrl});
When a client connects also binds his socket.io-client to that url
var socket = io('192.168.1.101:3000', {path: window.location.pathname});
I don't have problems and everything works great.
When a client performs a particular action, server do
socket.broadcast.emit("foo"); //I made console.log here and it prints
client-side:
socket.on("foo", () => console.log("okay"));
The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.
I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
error
node.js websocket socket.io
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Node create a unique url and bind socket.io to it.
var io = require("socket.io").listen(server, {path: req.originalUrl});
When a client connects also binds his socket.io-client to that url
var socket = io('192.168.1.101:3000', {path: window.location.pathname});
I don't have problems and everything works great.
When a client performs a particular action, server do
socket.broadcast.emit("foo"); //I made console.log here and it prints
client-side:
socket.on("foo", () => console.log("okay"));
The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.
I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
error
node.js websocket socket.io
Node create a unique url and bind socket.io to it.
var io = require("socket.io").listen(server, {path: req.originalUrl});
When a client connects also binds his socket.io-client to that url
var socket = io('192.168.1.101:3000', {path: window.location.pathname});
I don't have problems and everything works great.
When a client performs a particular action, server do
socket.broadcast.emit("foo"); //I made console.log here and it prints
client-side:
socket.on("foo", () => console.log("okay"));
The problem is that client-side "foo" event is almost never fired. Sometimes it is fired but only in particular occations. For example it happened that a socket.io-client auto-reconnect to server and then the event is fired.
I don't know if the problem is related to this, because for example socket.emit works, but when another client connects I always get this
error
node.js websocket socket.io
node.js websocket socket.io
asked Nov 11 at 17:07
Giulio
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
socket.broadcast.emit("foo");
broadcasts a message to all connected clients EXCEPT the one specified by socket
. If you want to broadcast to all connected clients, then use:
io.broadcast.emit("foo");
You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
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',
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%2f53251130%2fsocket-broadcast-emit-doesnt-fire-correctly%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
up vote
0
down vote
socket.broadcast.emit("foo");
broadcasts a message to all connected clients EXCEPT the one specified by socket
. If you want to broadcast to all connected clients, then use:
io.broadcast.emit("foo");
You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
add a comment |
up vote
0
down vote
socket.broadcast.emit("foo");
broadcasts a message to all connected clients EXCEPT the one specified by socket
. If you want to broadcast to all connected clients, then use:
io.broadcast.emit("foo");
You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
add a comment |
up vote
0
down vote
up vote
0
down vote
socket.broadcast.emit("foo");
broadcasts a message to all connected clients EXCEPT the one specified by socket
. If you want to broadcast to all connected clients, then use:
io.broadcast.emit("foo");
You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.
socket.broadcast.emit("foo");
broadcasts a message to all connected clients EXCEPT the one specified by socket
. If you want to broadcast to all connected clients, then use:
io.broadcast.emit("foo");
You will also have to make sure that your clients are correctly connected and not regularly losing and re-establishing their connections (you can see if that is happening by logging the connection and disconnection events on the server). If they are losing their connections somehow and then reconnecting, then which ones would get the broadcast message would happen to depend upon which ones were not in the middle of a temporarily lost connection.
answered Nov 11 at 17:27
jfriend00
425k51542589
425k51542589
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
add a comment |
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
yes, if I have two clients, and one of them perform the action, the other one would fire the foo event. This doesn't happen.
– Giulio
Nov 11 at 17:49
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53251130%2fsocket-broadcast-emit-doesnt-fire-correctly%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