Home > Blockchain >  Node Websocket: Best way to send large array data to clients
Node Websocket: Best way to send large array data to clients

Time:11-22

I have a websocket server made with express in nodejs

Websocket documentation states that websocket.send() can pass string or array buffer. Now I want to send a big array of objects (200k lines when formatted) over websocket to clients. What is the best way to send up data such as this.

What I've tried: I've tried sending it directly by stringifying it, Now this works completely fine but delay is very long.

So is there any way to send such a massive array data to clients while keeping speed intact? I think array buffers might help but I couldn't find any suggested examples

Also if this issue is code specific then let me know in comment so that I can share code snippets as well.

CodePudding user response:

Usually sockets are used to handle real time messaging, sacrificing common http features with the goal of to be light and fast. Check:

Transfer large amount of data goes against this. Check :

Advice 1

Use socket to detect the real time notification of some change in crypto ticker should use socket.

After tha when client knows (thank to the socket) that there are a new or and updated crypto ticker (which is large), download it using common http instead socket as @jfriend00 also recommends in comments.

Also if data is large, you should use an approach to split the data and send chunk by chunk using common http, not sockets.

Advice 2

As @jfriend00 said and also the major file host services do, implement and algorithm to split the data and send part by part to the client.

If the chunk or part is small, maybe you could use sockets but this is a common feature , so use the known way: common http

  • Related