1

I kinda understand that Websocket is the protocol that is used for real-time data flowing back & forth.

My question can be very pre-mature but couldn't find much help on the web.

Say 1000 clients are connected to a server which sends out real-time stock prices. When there is an update on the server front, how will server know all the 1000 clients to which it needs to send an update?

If this is some sort of looping that happens on the server side where all connected clients details are cached & then update will be sent out to all of them, isn't is an overhead ?

This SOF answer made some sense but didn't clear my doubt.

BeingSuman
  • 3,015
  • 7
  • 30
  • 48

2 Answers2

3

How does Server keep track of all Client(s) connected in Real time data pushing scenario?

It doesn't... it only keeps track of the clients it's serving specifically.

This answer is not node.js specific.

Say 1000 clients are connected to a server which sends out real-time stock prices. When there is an update on the server front, how will server know all the 1000 clients to which it needs to send an update?

To actually understand this a little better, we should consider larger numbers. i.e., let's assume 1 million clients connected to a service.

Obviously, a sane design will require redundancy, so no single service will hold all 1 million connections (and if a single server instance fails, clients can re-connect to a different server instance).

In this case, there's no single server that is aware of all clients.

It makes more sense for each server to manage it's own internal subscription / client list. Each server will also act as a pub/sub client for a centralized pub/sub service (such as a Redis cluster or whatever).

Assuming 1000 server instances, each serving 1000 clients, we would find that the pub/sub service is aware only of 1,000 "clients" (server instances). Each server is unaware of the other clients, it's only aware of the 1,000 clients it's managing.

If this is some sort of looping that happens on the server side where all connected clients details are cached & then update will be sent out to all of them, isn't is an overhead?

The algorithm itself is implementation specific, but in general, each server will incur some overhead in order to manage the pub/sub layer.

However, since each server only manages a small subset of the total client count, the overhead is distributed across a number of systems.

Channel Oriented vs. Connection Oriented Design

I should probably note that the pub/sub design isn't connection oriented.

The server isn't (or shouldn't be) looping over all the connections asking "are you subscribed to this channel"?.

Rather, pub/sub design assumes a "channel" oriented design, where it locates the channel object(s) and loops over a client list.

On one hand, this approach might (or might not) consume more memory. Since each "channel" should contain a list of clients listening to that channel, a single client object might belong to more than a single list.

On the other hand, the loop has less code branches and experiences less overhead than a connection oriented design. Also, this approach allows for pub/sub clients that aren't connection bound (such as internal hooks / callbacks).

Myst
  • 18,516
  • 2
  • 45
  • 67
2

Say 1000 clients are connected to a server which sends out real-time stock prices. When there is an update on the server front, how will server know all the 1000 clients to which it needs to send an update?

Socket.io already keeps track by itself and its pretty easy to emit to all connected clients.

Socket.io - Emit Cheatsheet

If you are worried about what would happen when your user-base grows, you can scale your service to multiple nodes.

If you actually end up scaling and have more than one server node, then you can use socketio-redis.

Adapter to enable broadcasting of events to multiple separate socket.io server nodes.
EMX
  • 6,066
  • 1
  • 25
  • 32
  • that kinda answers my question but will mark it as the accepted answer once i try socket.io from my end. Thanks a lot :) – BeingSuman Jun 26 '19 at 13:45