Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thrift transport in javascript client

I'm trying to build a javascript client for my Thrift server. The server is up and running and I can get calls to the server working with a PHP client. I just can't figure out the javascript client.

In particular, instantiating the transport baffles me. The tutorial at http://thrift.apache.org/tutorial/js/ shows:

function calc() {
    var transport = new Thrift.Transport("/thrift/service/tutorial/");
    var protocol  = new Thrift.Protocol(transport);
    var client    = new CalculatorClient(protocol);

When I do this, I get an http 404 on "/var/www/thrift/service/tutorial/"

I've found one or two other examples that use

var transport = new Thrift.Transport("/service");

But that gives me a 404 as well.

I've never seen an explanation of what I'm supposed to pass to the constructor of Transport in javascript. In my PHP code, I create a socket and then pass that to the constructor of the Transport. However, javascript complains that Thrift.Socket() isn't a constructor.

The tutorial at http://thrift.apache.org/tutorial/js/ isn't terribly helpful. It says:

The first thing for using the Thrift files is setting up your Transport protocol. At this time, it only supports AJAX and is as follows:

var transport = new Thrift.Transport("/thrift/service/tutorial/");

There's no description of what that path passed to the constructor should be.

I'm lost on this. What do I pass to the Transport constructor in javascript?

like image 482
The Demigeek Avatar asked May 03 '13 03:05

The Demigeek


2 Answers

To use a Javascript client you need a HTTP Thrift server (e.g. the one pointed out by yiding). You also need to use TJSONProtocol.

The files how to make a Thrift Java server work with an Thrift Javascript client are scattered throughout the source. I puzzled them together here: https://github.com/LukeOwncloud/ThriftJavaJavascriptDemo

like image 130
Jack Miller Avatar answered Oct 14 '22 17:10

Jack Miller


The argument is a URL to a website endpoint that acts as a thrift server using the HTTP processor and JSON protocol. The source code contains a java test server example which can work with such a client.

For your own server, it should be able to act as a webserver, and handle things like CORS for cross-domain requests from your js client.

like image 29
yiding Avatar answered Oct 14 '22 15:10

yiding