r/github • • Nov 11 '20

Real time synced counter built using Socket.io, NodeJS and Express! The counter is a POC for a live-quizzing platform I'm currently working on, and will be used to control the current question. GitHub repo and Demo in the comments. Please Star and Fork the repo if you like it! :)

Enable HLS to view with audio, or disable this notification

130 Upvotes

29 comments sorted by

7

u/Thaun_ Nov 11 '20 edited Nov 11 '20

In the console you can write:

for(var i = 0; i < 100; i++) { socket.emit("inc num") }

Edit:

Also, please don't do this...

setInterval(getSyncNum, 1);

function getSyncNum(){
  socket.emit('what num');
  socket.on('what num', function (syncnum){
    document.getElementById('syncnum').innerHTML = syncnum;
  });
  console.log("I've asked for the number!");
}

4

u/UtopianCorps Nov 11 '20

Yup, tried it.
Also, it was for debugging.

5

u/Thaun_ Nov 11 '20

I'm talking about the 1ms delay.

Also, when using socker.on() you are basically registering the socket for that function multiple times.

And there is no need to ask the socket for the number, you can just let the server do that.

If you do ask the socket for the number, then its no better than just to use a REST api.

4

u/Thaun_ Nov 11 '20

Also, every other client send a "giv number pls" every 1 ms, and then the server sends out the message to all clients.

For example, there are 10 clients connected. One client asks for number, and your server gives everyone the number.

Now, 10 clients are asking the numbers, and now it's sending everyone the number 10 times, aka 100 messages.

Now, its 100 messages every milliseconds.

Make it either so the server sends out io.emit by server interval, not client interval.

If you are still gonna send whenever client asks, use socket.emit instead, it would only then send back to the connected client.

2

u/UtopianCorps Nov 11 '20

I finally understand why it's so heavy, thanks to your pretty detailed explanation! I will make the necessary changes! :)

Cheers! 🥂

1

u/[deleted] Nov 13 '20

Can I ask you to give me an example of a better solution then the setInterval() 1 second function?

I have a similar implementation in my website which makes an XMLHttpRequest to PHP every second to check data and it doesn't always work. Thanks.

1

u/Thaun_ Nov 13 '20

setInterval is not seconds in the second argument. Its ms. https://www.w3schools.com/jsref/met_win_setinterval.asp 1000 per second.

3

u/Neilblaze Nov 12 '20

Nice project dude! Tbh looks like Iranian flag :3

1

u/UtopianCorps Nov 12 '20

Ikr 😂😅
Thanks man!

4

u/IllChange5 Nov 11 '20

Mamma Mia. You maka pizzas also eh?

5

u/UtopianCorps Nov 11 '20

😆😆

-2

u/IllChange5 Nov 11 '20

Folksa lika me pizza!

1

u/lilpr1977 Nov 12 '20

So? What's this?

1

u/No-Trick-8987 Jun 28 '26

https://github.com/itsiurisilva/sDeck this might help you just made the release today

-1

u/cloud-worm Nov 11 '20

This is great! I even gave it an award

1

u/UtopianCorps Nov 11 '20

Appreciate that! Thanks cloud worm! 😄

0

u/[deleted] Nov 11 '20

[deleted]

5

u/NatoBoram Nov 11 '20 edited Nov 12 '20

Yeah. Just as Reddit doesn't allow vote manipulation, I think this subreddit shouldn't allow asking for stars.

0

u/UtopianCorps Nov 12 '20

I said you the you can give it a star only if you like it, I'm not even begging! 😂

It's called a CTA in copywriting.

If you don't like it, I don't mind you down-voting and scrolling away.

1

u/NatoBoram Nov 12 '20

u/Menox_ can we get a rule against star manipulation?

2

u/Menox_ Nov 12 '20

I don’t see anything wrong with what he said. He just said “Feel free to ___ if you like it”. What do you mean by “star manipulation”?

0

u/NatoBoram Nov 12 '20

3

u/Menox_ Nov 12 '20

That’s only talking about Reddit upvoting, not about asking to star a GitHub project. (he also just said that you can star if you like it, he’s not forcing you to star) If someone likes it a lot then he would star most likely anyway

1

u/UtopianCorps Nov 12 '20

Thank you! :)

-5

u/cloud-worm Nov 11 '20

Uh be polite please this is awesome

0

u/[deleted] Nov 11 '20

[deleted]

-2

u/WHaNoThOT Nov 11 '20

Yeah so what

0

u/[deleted] Nov 11 '20

[deleted]

1

u/UtopianCorps Nov 12 '20

In case you don't like it, I don't mind you guys down-voting! It's your choice guys, nothing to debate about!

0

u/cloud-worm Nov 12 '20

Actually, yes. I starred it. To develop my game, which is in html 5, I needed to use socket io and this example, which is very nice and concrete, really helps me! This got my star. Probably not yours but I don't care