Node.js: zlib Compression in a tcp connection

I am new to node.js and currently working on a tcp server. I want all the messages to be sent to client in a compressed format.I am using zlib for this purpose.

The following sample is a code on server side:

zlib.deflate(response.toString(), function(err, buffer) {
          if (!err) {
            session.xmlSocket.writeString(buffer);
            }
          }); 

This function basically compresses the data and writes it to the socket.The code on client side that decompress that data is:

this.socket.on("data", function(chunk){
     zlib.unzip(chunk, function(err, buffer) {
     if (!err) {
            self.parser.write(buffer.toString());
        console.log(buffer.toString());
      }
    });

Now this code works fine when I run both client and server on the same pc.But is it correct to directly write the Buffer object returned by the zlib.deflate function and write it on the socket and then use it on the client side?

Don't send the raw object. Try buffer.toString().

session.xmlSocket.writeString(buffer.toString());