blob: 07b188ba723aa945418a7fa397e2ab0ef179ac21 [file] [log] [blame]
T Jake Luciani322caa22010-02-15 03:24:55 +00001Thrift Javascript Library
Henrique Mendonça095ddb72013-09-20 19:38:03 +02002=========================
3This browser based Apache Thrift implementation supports
henrique2a7dccc2014-03-07 22:16:51 +01004RPC clients using the JSON protocol over Http[s] with XHR
5and WebSocket.
T Jake Luciani322caa22010-02-15 03:24:55 +00006
7License
Henrique Mendonça095ddb72013-09-20 19:38:03 +02008-------
T Jake Luciani322caa22010-02-15 03:24:55 +00009Licensed to the Apache Software Foundation (ASF) under one
10or more contributor license agreements. See the NOTICE file
11distributed with this work for additional information
12regarding copyright ownership. The ASF licenses this file
13to you under the Apache License, Version 2.0 (the
14"License"); you may not use this file except in compliance
15with the License. You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19Unless required by applicable law or agreed to in writing,
20software distributed under the License is distributed on an
21"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22KIND, either express or implied. See the License for the
23specific language governing permissions and limitations
24under the License.
25
henrique2a7dccc2014-03-07 22:16:51 +010026Grunt Build
27------------
28The is the base directory for the Apache Thrift JavaScript
29library. This directory contains a Gruntfile.js and a
30package.json. Many of the build and test tools used here
31require a recent version of Node.js to be installed. To
32install the support files for the Grunt build tool execute
33the command:
34 $ npm install
35This reads the package.json and pulls in the appropriate
36sources from the internet. To build the JavaScript branch
37of Apache Thrift execute the command:
38 $ grunt
39This runs the grunt build tool, linting all of the source
40files, setting up and running the tests, concatenating and
41minifying the main libraries and generating the html
42documentation.
43
44Tree
45----
46The following directories are present (some only after the
47grunt build):
48 /src - The JavaScript Apache Thrift source
49 /doc - HTML documentation
50 /dist - Distribution files (thrift.js and thrift.min.js)
51 /test - Various tests, this is a good place to look for
52 example code
53 /node_modules - Build support files installed by npm
54
55
56Example JavaScript Client and Server
57------------------------------------
Henrique Mendonça095ddb72013-09-20 19:38:03 +020058The listing below demonstrates a simple browser based JavaScript
henriquea2de4102014-02-07 14:12:56 +010059Thrift client and Node.js JavaScript server for the hello_svc
60service.
T Jake Luciani322caa22010-02-15 03:24:55 +000061
henrique2a7dccc2014-03-07 22:16:51 +010062### hello.thrift - Service IDL
63### build with: $ thrift -gen js -gen js:node hello.thrift
henriquea2de4102014-02-07 14:12:56 +010064 service hello_svc {
65 string get_message(1: string name)
Henrique Mendonça095ddb72013-09-20 19:38:03 +020066 }
T Jake Luciani322caa22010-02-15 03:24:55 +000067
Henrique Mendonça095ddb72013-09-20 19:38:03 +020068### hello.html - Browser Client
henriquea2de4102014-02-07 14:12:56 +010069 <!DOCTYPE html>
Henrique Mendonça095ddb72013-09-20 19:38:03 +020070 <html lang="en">
henriquea2de4102014-02-07 14:12:56 +010071 <head>
72 <meta charset="utf-8">
73 <title>Hello Thrift</title>
74 </head>
75 <body>
76 Name: <input type="text" id="name_in">
77 <input type="button" id="get_msg" value="Get Message" >
78 <div id="output"></div>
79
80 <script src="thrift.js"></script>
81 <script src="gen-js/hello_svc.js"></script>
82 <script>
83 (function() {
84 var transport = new Thrift.Transport("/hello");
85 var protocol = new Thrift.Protocol(transport);
86 var client = new hello_svcClient(protocol);
87 var nameElement = document.getElementById("name_in");
88 var outputElement = document.getElementById("output");
89 document.getElementById("get_msg")
90 .addEventListener("click", function(){
91 client.get_message(nameElement.value, function(result) {
92 outputElement.innerHTML = result;
93 });
94 });
95 })();
Henrique Mendonça095ddb72013-09-20 19:38:03 +020096 </script>
henriquea2de4102014-02-07 14:12:56 +010097 </body>
Henrique Mendonça095ddb72013-09-20 19:38:03 +020098 </html>
T Jake Luciani322caa22010-02-15 03:24:55 +000099
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200100### hello.js - Node Server
henriquea2de4102014-02-07 14:12:56 +0100101 var Thrift = require('thrift');
102 var TBufferedTransport = require('thrift/lib/thrift/transport').TBufferedTransport;
103 var TJSONProtocol = require('thrift/lib/thrift/protocol').TJSONProtocol;
104 var hello_svc = require('./gen-nodejs/hello_svc.js');
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200105
henriquea2de4102014-02-07 14:12:56 +0100106 var hello_handler = {
107 get_message: function(name, result) {
108 var msg = "Hello " + name + "!";
109 result(null, msg);
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200110 }
henriquea2de4102014-02-07 14:12:56 +0100111 }
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200112
henriquea2de4102014-02-07 14:12:56 +0100113 var hello_svc_opt = {
114 transport: TBufferedTransport,
115 protocol: TJSONProtocol,
116 cls: hello_svc,
117 handler: hello_handler
118 };
119
120 var server_opt = {
121 staticFilePath: ".",
122 services: {
123 "/hello": hello_svc_opt
124 }
125 }
126
henrique2a7dccc2014-03-07 22:16:51 +0100127 var server = Thrift.createThriftWebServer(server_opt);
henriquea2de4102014-02-07 14:12:56 +0100128 var port = 9099;
129 server.listen(port);
henrique2a7dccc2014-03-07 22:16:51 +0100130 console.log("Http/Thrift Server running on port: " + port);
131