Search code examples
node.jsserializationdeserializationthrift

Apache Thrift with nodejs example


I am trying to use Apache Thrift for passing messages between applications implemented in different languages. It is not necessarily used as RPC, but more for serializing/deserializing messages. One application is in node.js. I am trying to figure out how Apache thrift works with node.js, but I can't find too much documentation and examples, except for one tiny one regarding Cassandra at: https://github.com/apache/thrift/tree/trunk/lib/nodejs

Again, I don't need any procedures declared in the .thrift file, I only need to serialize a simple data structure like:

struct Notification {
   1: string subject,
   2: string message
 }

Can anyone help me with an example?


Solution

  • The above answer is wrong, because it tries to use outBuffers directly, which is an array of buffers. Here is a working example of using thrift with nodejs:

    var util = require('util');
    var thrift = require('thrift');
    
    var Notification = require('./gen-nodejs/notification_types.js').Notification;
    
    var TFramedTransport = require('thrift/lib/thrift/transport').TFramedTransport;
    var TBufferedTransport = require('thrift/lib/thrift/transport').TBufferedTransport;
    var TBinaryProtocol = require('thrift/lib/thrift/protocol').TBinaryProtocol;
    
    var transport = new TFramedTransport(null, function(byteArray) {
      // Flush puts a 4-byte header, which needs to be parsed/sliced.
      byteArray = byteArray.slice(4);
    
      // DESERIALIZATION:
      var tTransport = new TFramedTransport(byteArray);
      var tProtocol = new TBinaryProtocol(tTransport);
      var receivedNotification = new Notification();
      receivedUser.read(tProtocol);
    
      console.log(util.inspect(receivedNotification, false, null));
    });
    
    var binaryProt = new TBinaryProtocol(transport);
    
    // SERIALIZATION:
    var notification = new Notification({"subject":"AAAA"});
    console.log(util.inspect(notification, false, null));
    notification.write(binaryProt);
    transport.flush();