My SocketIO script is always sending the same data












0















I have this Python variable called tst, which is equal to 2. I want to send the value of this variable to my frontend to my frontend using SocketIO.



The code is working, the only problem is that instead of storing a single two in my javascript array, it will keep filling the array with 2 each time an update is sent. The javascript array should look like this [2] (i will be sending other values, this is just a test), but instead it looks like this



[2, 2, 2, 2, 2, 2, ........ 2]


Is there a way to fix this?



SocketIO:



 class RandomThread(Thread):
def __init__(self):
self.delay = 1
super(RandomThread, self).__init__()

def randomNumberGenerator(self):
"""
Generate a random number every 1 second and emit to a socketio instance (broadcast)
Ideally to be run in a separate thread?
"""
#infinite loop of magical random numbers
print("Making random numbers")
while not thread_stop_event.isSet():
number = round(random()*10, 3)

tst = 2

print(number)
socketio.emit('newnumber', {'number': number}, namespace='/test')

socketio.emit('two', {'num': tst}, namespace='/test')

sleep(self.delay)



def run(self):
self.randomNumberGenerator()


application.js



socket.on('two', function(msg) {
console.log("Received" + msg.num);

arr.push(msg.num)

console.log(arr);
});









share|improve this question























  • That's exactly what .push() does. What behavior did you expect in details?

    – Klaus D.
    Nov 24 '18 at 12:56











  • Well, i only want to have 2 one time, in the future i will have some data each second instead of that "2", and i want to have only new data. This way, it will keep printing the same data over and over

    – Jack022
    Nov 24 '18 at 14:55
















0















I have this Python variable called tst, which is equal to 2. I want to send the value of this variable to my frontend to my frontend using SocketIO.



The code is working, the only problem is that instead of storing a single two in my javascript array, it will keep filling the array with 2 each time an update is sent. The javascript array should look like this [2] (i will be sending other values, this is just a test), but instead it looks like this



[2, 2, 2, 2, 2, 2, ........ 2]


Is there a way to fix this?



SocketIO:



 class RandomThread(Thread):
def __init__(self):
self.delay = 1
super(RandomThread, self).__init__()

def randomNumberGenerator(self):
"""
Generate a random number every 1 second and emit to a socketio instance (broadcast)
Ideally to be run in a separate thread?
"""
#infinite loop of magical random numbers
print("Making random numbers")
while not thread_stop_event.isSet():
number = round(random()*10, 3)

tst = 2

print(number)
socketio.emit('newnumber', {'number': number}, namespace='/test')

socketio.emit('two', {'num': tst}, namespace='/test')

sleep(self.delay)



def run(self):
self.randomNumberGenerator()


application.js



socket.on('two', function(msg) {
console.log("Received" + msg.num);

arr.push(msg.num)

console.log(arr);
});









share|improve this question























  • That's exactly what .push() does. What behavior did you expect in details?

    – Klaus D.
    Nov 24 '18 at 12:56











  • Well, i only want to have 2 one time, in the future i will have some data each second instead of that "2", and i want to have only new data. This way, it will keep printing the same data over and over

    – Jack022
    Nov 24 '18 at 14:55














0












0








0








I have this Python variable called tst, which is equal to 2. I want to send the value of this variable to my frontend to my frontend using SocketIO.



The code is working, the only problem is that instead of storing a single two in my javascript array, it will keep filling the array with 2 each time an update is sent. The javascript array should look like this [2] (i will be sending other values, this is just a test), but instead it looks like this



[2, 2, 2, 2, 2, 2, ........ 2]


Is there a way to fix this?



SocketIO:



 class RandomThread(Thread):
def __init__(self):
self.delay = 1
super(RandomThread, self).__init__()

def randomNumberGenerator(self):
"""
Generate a random number every 1 second and emit to a socketio instance (broadcast)
Ideally to be run in a separate thread?
"""
#infinite loop of magical random numbers
print("Making random numbers")
while not thread_stop_event.isSet():
number = round(random()*10, 3)

tst = 2

print(number)
socketio.emit('newnumber', {'number': number}, namespace='/test')

socketio.emit('two', {'num': tst}, namespace='/test')

sleep(self.delay)



def run(self):
self.randomNumberGenerator()


application.js



socket.on('two', function(msg) {
console.log("Received" + msg.num);

arr.push(msg.num)

console.log(arr);
});









share|improve this question














I have this Python variable called tst, which is equal to 2. I want to send the value of this variable to my frontend to my frontend using SocketIO.



The code is working, the only problem is that instead of storing a single two in my javascript array, it will keep filling the array with 2 each time an update is sent. The javascript array should look like this [2] (i will be sending other values, this is just a test), but instead it looks like this



[2, 2, 2, 2, 2, 2, ........ 2]


Is there a way to fix this?



SocketIO:



 class RandomThread(Thread):
def __init__(self):
self.delay = 1
super(RandomThread, self).__init__()

def randomNumberGenerator(self):
"""
Generate a random number every 1 second and emit to a socketio instance (broadcast)
Ideally to be run in a separate thread?
"""
#infinite loop of magical random numbers
print("Making random numbers")
while not thread_stop_event.isSet():
number = round(random()*10, 3)

tst = 2

print(number)
socketio.emit('newnumber', {'number': number}, namespace='/test')

socketio.emit('two', {'num': tst}, namespace='/test')

sleep(self.delay)



def run(self):
self.randomNumberGenerator()


application.js



socket.on('two', function(msg) {
console.log("Received" + msg.num);

arr.push(msg.num)

console.log(arr);
});






javascript python socket.io






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 12:50









Jack022Jack022

5510




5510













  • That's exactly what .push() does. What behavior did you expect in details?

    – Klaus D.
    Nov 24 '18 at 12:56











  • Well, i only want to have 2 one time, in the future i will have some data each second instead of that "2", and i want to have only new data. This way, it will keep printing the same data over and over

    – Jack022
    Nov 24 '18 at 14:55



















  • That's exactly what .push() does. What behavior did you expect in details?

    – Klaus D.
    Nov 24 '18 at 12:56











  • Well, i only want to have 2 one time, in the future i will have some data each second instead of that "2", and i want to have only new data. This way, it will keep printing the same data over and over

    – Jack022
    Nov 24 '18 at 14:55

















That's exactly what .push() does. What behavior did you expect in details?

– Klaus D.
Nov 24 '18 at 12:56





That's exactly what .push() does. What behavior did you expect in details?

– Klaus D.
Nov 24 '18 at 12:56













Well, i only want to have 2 one time, in the future i will have some data each second instead of that "2", and i want to have only new data. This way, it will keep printing the same data over and over

– Jack022
Nov 24 '18 at 14:55





Well, i only want to have 2 one time, in the future i will have some data each second instead of that "2", and i want to have only new data. This way, it will keep printing the same data over and over

– Jack022
Nov 24 '18 at 14:55












1 Answer
1






active

oldest

votes


















1














Your array is being filled because your Python code sends those 2's in a loop and your client code appends (pushes) it to the array. You have to clear the array or define it in the function scope. However I don't know why do you want a single number in an array as a result. If you want a single random number from the server every second, one variable is enough. On the other hand, if you need array of random numbers, it would be better to prepare it on the server side and then send the whole thing through socket. I have a feeling that's the XY problem.






share|improve this answer
























  • Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

    – Jack022
    Nov 24 '18 at 14:57











  • It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

    – Adrian
    Nov 24 '18 at 15:11













  • I just checked and for some reason it seems to send the same number 4 times.

    – Jack022
    Nov 24 '18 at 16:40











  • I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

    – Jack022
    Nov 24 '18 at 16:40











  • I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

    – Adrian
    Nov 24 '18 at 17:04











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%2f53458333%2fmy-socketio-script-is-always-sending-the-same-data%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














Your array is being filled because your Python code sends those 2's in a loop and your client code appends (pushes) it to the array. You have to clear the array or define it in the function scope. However I don't know why do you want a single number in an array as a result. If you want a single random number from the server every second, one variable is enough. On the other hand, if you need array of random numbers, it would be better to prepare it on the server side and then send the whole thing through socket. I have a feeling that's the XY problem.






share|improve this answer
























  • Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

    – Jack022
    Nov 24 '18 at 14:57











  • It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

    – Adrian
    Nov 24 '18 at 15:11













  • I just checked and for some reason it seems to send the same number 4 times.

    – Jack022
    Nov 24 '18 at 16:40











  • I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

    – Jack022
    Nov 24 '18 at 16:40











  • I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

    – Adrian
    Nov 24 '18 at 17:04
















1














Your array is being filled because your Python code sends those 2's in a loop and your client code appends (pushes) it to the array. You have to clear the array or define it in the function scope. However I don't know why do you want a single number in an array as a result. If you want a single random number from the server every second, one variable is enough. On the other hand, if you need array of random numbers, it would be better to prepare it on the server side and then send the whole thing through socket. I have a feeling that's the XY problem.






share|improve this answer
























  • Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

    – Jack022
    Nov 24 '18 at 14:57











  • It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

    – Adrian
    Nov 24 '18 at 15:11













  • I just checked and for some reason it seems to send the same number 4 times.

    – Jack022
    Nov 24 '18 at 16:40











  • I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

    – Jack022
    Nov 24 '18 at 16:40











  • I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

    – Adrian
    Nov 24 '18 at 17:04














1












1








1







Your array is being filled because your Python code sends those 2's in a loop and your client code appends (pushes) it to the array. You have to clear the array or define it in the function scope. However I don't know why do you want a single number in an array as a result. If you want a single random number from the server every second, one variable is enough. On the other hand, if you need array of random numbers, it would be better to prepare it on the server side and then send the whole thing through socket. I have a feeling that's the XY problem.






share|improve this answer













Your array is being filled because your Python code sends those 2's in a loop and your client code appends (pushes) it to the array. You have to clear the array or define it in the function scope. However I don't know why do you want a single number in an array as a result. If you want a single random number from the server every second, one variable is enough. On the other hand, if you need array of random numbers, it would be better to prepare it on the server side and then send the whole thing through socket. I have a feeling that's the XY problem.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 13:30









AdrianAdrian

8815




8815













  • Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

    – Jack022
    Nov 24 '18 at 14:57











  • It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

    – Adrian
    Nov 24 '18 at 15:11













  • I just checked and for some reason it seems to send the same number 4 times.

    – Jack022
    Nov 24 '18 at 16:40











  • I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

    – Jack022
    Nov 24 '18 at 16:40











  • I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

    – Adrian
    Nov 24 '18 at 17:04



















  • Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

    – Jack022
    Nov 24 '18 at 14:57











  • It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

    – Adrian
    Nov 24 '18 at 15:11













  • I just checked and for some reason it seems to send the same number 4 times.

    – Jack022
    Nov 24 '18 at 16:40











  • I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

    – Jack022
    Nov 24 '18 at 16:40











  • I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

    – Adrian
    Nov 24 '18 at 17:04

















Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

– Jack022
Nov 24 '18 at 14:57





Let me explain it further: in the future, intead of that "2" i will have some other numbers to push to my Javascript frontend. Those numbers pushed on the frontend will be charted in real time with a Javascript charting library. The problem is that, with this situation, it will keep sending the same data over and over again, when i'll want to have only NEW data

– Jack022
Nov 24 '18 at 14:57













It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

– Adrian
Nov 24 '18 at 15:11







It doesn't send the same data. I mean it does, but only because you told it so send "2". If those numbers were random, it should work like you want it to work. Unless you want to send more specific data than random numbers. Then it's up to your server to generate this data and send it. Perhaps your thread can quit by itself after it sends the data, instead of being notified by an event.

– Adrian
Nov 24 '18 at 15:11















I just checked and for some reason it seems to send the same number 4 times.

– Jack022
Nov 24 '18 at 16:40





I just checked and for some reason it seems to send the same number 4 times.

– Jack022
Nov 24 '18 at 16:40













I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

– Jack022
Nov 24 '18 at 16:40





I debugged it with a console.log(arr) and i noticed that the same number seemed to be added to the array multiple times

– Jack022
Nov 24 '18 at 16:40













I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

– Adrian
Nov 24 '18 at 17:04





I can only make guesses. Maybe you your random function returns a number twice in a row, it's perfectly possible. Do you have multiple threads running? I don't think random() is thread safe.

– Adrian
Nov 24 '18 at 17:04


















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%2f53458333%2fmy-socketio-script-is-always-sending-the-same-data%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)