Akram Kamal Qassas
Akram Kamal Qassas

Reputation: 509

compress/decompress string in node.js and javascript client side

I have a node server that will serve more than 10,000 users at the same time with big data, I already tried lz-string https://www.npmjs.org/package/lz-string but it's not good module because it's blocking the node thread.

Please answer these questions:

  1. is it better to compress the data in server and then decompress in client instead of send plain/json data?

  2. what is the best and fastest way to compress/decompress the data?

Upvotes: 1

Views: 3180

Answers (1)

alandarev
alandarev

Reputation: 8635

If you are sending large chunks of text data over the internet using HTTP protocol, then there are already some technologies in place to help you.

One is called HTTP Compression. HTTP protocol specifications allow few compression algorithms to perform on data being sent, but that requires the server and client to be properly configured for compression. Standard Node.js server will not compress the data without modifying code.


For bare Node.js without any frameworks and 3rd party modules there is zlib module made specially for HTTP compression, both server and client.


Using Express? Then there is a compression middleware.


It might also be worth looking using nginx as a proxy server to your node.js applications. Then you can easily flip the switch ON for compression in nginx, without needing to do anything in your Node.js application at all:

server {
  gzip on;
  ...
}

It really depends on the stack you are using, but the idea is same: compress the HTTP stream itself, as it is supported by the protocol.

Upvotes: 3

Related Questions