blob: dfa6e82382c256074d27613bbad09e89d4deec7c [file] [log] [blame] [view]
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------------
Roger Meier52744ee2014-03-12 09:38:42 +010028This is the base directory for the Apache Thrift JavaScript
henrique2a7dccc2014-03-07 22:16:51 +010029library. 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:
henriqued0265c72014-05-19 19:11:10 +020034
35 npm install
36
henrique2a7dccc2014-03-07 22:16:51 +010037This reads the package.json and pulls in the appropriate
38sources from the internet. To build the JavaScript branch
39of Apache Thrift execute the command:
henriqued0265c72014-05-19 19:11:10 +020040
41 grunt
42
Randy Abernethyb2652302014-11-15 12:02:57 -080043This runs the grunt build tool, linting all of the source
henrique2a7dccc2014-03-07 22:16:51 +010044files, setting up and running the tests, concatenating and
45minifying the main libraries and generating the html
46documentation.
47
Randy Abernethyb2652302014-11-15 12:02:57 -080048If grunt is not installed you can install it with npm
49like this:
50
51 sudo npm install -g grunt-cli
52 npm install grunt --save-dev
53
54
henrique2a7dccc2014-03-07 22:16:51 +010055Tree
56----
57The following directories are present (some only after the
58grunt build):
59 /src - The JavaScript Apache Thrift source
Roger Meier52744ee2014-03-12 09:38:42 +010060 /doc - HTML documentation
henrique2a7dccc2014-03-07 22:16:51 +010061 /dist - Distribution files (thrift.js and thrift.min.js)
62 /test - Various tests, this is a good place to look for
63 example code
64 /node_modules - Build support files installed by npm
65
66
67Example JavaScript Client and Server
68------------------------------------
Henrique Mendonça095ddb72013-09-20 19:38:03 +020069The listing below demonstrates a simple browser based JavaScript
henriquea2de4102014-02-07 14:12:56 +010070Thrift client and Node.js JavaScript server for the hello_svc
71service.
T Jake Luciani322caa22010-02-15 03:24:55 +000072
henrique2a7dccc2014-03-07 22:16:51 +010073### hello.thrift - Service IDL
74### build with: $ thrift -gen js -gen js:node hello.thrift
henriquea2de4102014-02-07 14:12:56 +010075 service hello_svc {
76 string get_message(1: string name)
Henrique Mendonça095ddb72013-09-20 19:38:03 +020077 }
T Jake Luciani322caa22010-02-15 03:24:55 +000078
Henrique Mendonça095ddb72013-09-20 19:38:03 +020079### hello.html - Browser Client
henriquea2de4102014-02-07 14:12:56 +010080 <!DOCTYPE html>
Henrique Mendonça095ddb72013-09-20 19:38:03 +020081 <html lang="en">
henriquea2de4102014-02-07 14:12:56 +010082 <head>
83 <meta charset="utf-8">
84 <title>Hello Thrift</title>
85 </head>
86 <body>
87 Name: <input type="text" id="name_in">
88 <input type="button" id="get_msg" value="Get Message" >
89 <div id="output"></div>
90
91 <script src="thrift.js"></script>
92 <script src="gen-js/hello_svc.js"></script>
93 <script>
94 (function() {
Roger Meier52744ee2014-03-12 09:38:42 +010095 var transport = new Thrift.TXHRTransport("/hello");
96 var protocol = new Thrift.TJSONProtocol(transport);
henriquea2de4102014-02-07 14:12:56 +010097 var client = new hello_svcClient(protocol);
98 var nameElement = document.getElementById("name_in");
99 var outputElement = document.getElementById("output");
100 document.getElementById("get_msg")
101 .addEventListener("click", function(){
102 client.get_message(nameElement.value, function(result) {
103 outputElement.innerHTML = result;
104 });
105 });
106 })();
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200107 </script>
henriquea2de4102014-02-07 14:12:56 +0100108 </body>
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200109 </html>
T Jake Luciani322caa22010-02-15 03:24:55 +0000110
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200111### hello.js - Node Server
Roger Meier52744ee2014-03-12 09:38:42 +0100112 var thrift = require('thrift');
henriquea2de4102014-02-07 14:12:56 +0100113 var hello_svc = require('./gen-nodejs/hello_svc.js');
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200114
henriquea2de4102014-02-07 14:12:56 +0100115 var hello_handler = {
116 get_message: function(name, result) {
117 var msg = "Hello " + name + "!";
118 result(null, msg);
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200119 }
henriquea2de4102014-02-07 14:12:56 +0100120 }
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200121
henriquea2de4102014-02-07 14:12:56 +0100122 var hello_svc_opt = {
Roger Meier52744ee2014-03-12 09:38:42 +0100123 transport: thrift.TBufferedTransport,
124 protocol: thrift.TJSONProtocol,
125 processor: hello_svc,
henriquea2de4102014-02-07 14:12:56 +0100126 handler: hello_handler
127 };
128
129 var server_opt = {
130 staticFilePath: ".",
131 services: {
132 "/hello": hello_svc_opt
133 }
134 }
135
Roger Meier52744ee2014-03-12 09:38:42 +0100136 var server = Thrift.createWebServer(server_opt);
henriquea2de4102014-02-07 14:12:56 +0100137 var port = 9099;
138 server.listen(port);
henrique2a7dccc2014-03-07 22:16:51 +0100139 console.log("Http/Thrift Server running on port: " + port);
140
henriqued0265c72014-05-19 19:11:10 +0200141
142TypeScript
143------------------------------------
144TypeScript definition files can also be generated by running:
145
146 thrift --gen js:ts file.thrift
147