Send socket.io response data to the client from node.js server in gzip format

The problem I am facing is that I do not know how to determine if the data that I send back to the client will be compressed in gzip format. Looking at the output of my server from the command line, I see:

debug - websocket writing 3:::{"result":1368673052397} debug - websocket writing 3:::{"result":1368673053399} ... 

It seems to me that the server writes the response in ascii form, and does not compress it before sending.

The following is an example that I wrote to get these results. From what I read as long as I installed the "gzip browser", my answers should be sent to gzip. If they are not the way I do it, and if I can somehow tell from the server debugging information, that they are actually compressed answers.

When I start the server, I use the following command in BASH:

$ NODE_ENV = production node app.js

 var express = require('express'), http = require('http'); var app = express(), server = http.createServer(app), io = require('socket.io').listen(server); io.configure('production', function() { io.enable('browser client minification'); io.enable('browser client etag'); io.enable('browser client gzip'); io.set('log level', 3); }); app.use(express.logger('dev')); app.get('/', function(req, res) { res.send( "<script src='/socket.io/socket.io.js'></script>\n"+ "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>\n"+ "<script>\n"+ "var socket=io.connect('http://127.0.0.1:3000');\n"+ "socket.on('message', function(data) {\n"+ " $(\"h2\").text(data);\n"+ "});\n"+ "</script>\n"+ "<h1>"+process.env.NODE_ENV+"</h1>\n"+ "<h2></h2>\n" ); }); server.listen('3000'); io.sockets.on('connection', function(webSocket) { function whileLoop() { setTimeout(function() { var epoch = (new Date).getTime(); var jsonData = "{\"result\":"+epoch+"}"; webSocket.send(jsonData); whileLoop(); }, 1000); } whileLoop(); }); 
+7
source share
4 answers

After reading some comments, I decided to look at third-party libraries for decompression processing on the client side, which led me to JSXCompressor.

http://jsxgraph.uni-bayreuth.de/wp/jsxcompressor/

JSXCompressor takes the encrypted data from the g64 base from the server and processes decompression and decoding. Just download the library and put it in the appropriate folder.

On the server side, I use zlib to handle gzipping.

 var express = require('express'), http = require('http') zlib = require('zlib'); var app = express(), server = http.createServer(app), io = require('socket.io').listen(server); app.use(express.logger('dev')); app.use(express.static(__dirname + '/public')); app.get('/', function(req, res) { res.send( "<script src='/socket.io/socket.io.js'></script>\n"+ "<script src='/java/jsxcompressor.min.js'></script>\n"+ "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>\n"+ "<script>\n"+ "var socket=io.connect('http://127.0.0.1:3000');\n"+ "socket.on('message', function(data) {\n"+ " var jsonData = JXG.decompress(data);"+ " $(\"h1\").text(jsonData);\n"+ "});\n"+ "</script>\n"+ "<h1></h1>\n" ); }); server.listen('3000'); io.sockets.on('connection', function(webSocket) { function whileLoop() { setTimeout(function() { var epoch = (new Date).getTime(); var jsonData = "{\"result\":"+epoch+"}"; zlib.gzip(jsonData, function(err, buffer) { webSocket.send(buffer.toString('base64')); }); whileLoop(); }, 1000); } whileLoop(); }); 
+2
source

The browser client gzip parameter enables browser client gzip compression for the socket.io script, which is served with /socket.io/socket.io.js . This does not affect the actual connection to WebSocket.

The WebSocket protocol itself has only recently added support for compressing data sent over a socket. Soket.io does not yet support compression , as well as other node WebSocket servers .

Honestly, with a small amount of data that you send in your example, compression will actually be counterproductive, as it is likely to increase the amount of data sent over the wire.

+5
source

Supported in socket.io 1.4 Compression is enabled by default.

+3
source

Compression is now enabled by default (> 1.4), see also this post for reference: http://socket.io/blog/socket-io-1-4-0/

+2
source

All Articles