iPandemic Con セて 01 Center S.ay 50m2 び気 00 ~ ・一 1 を ' 3 90 ミ 0 セ 0 be は 10 れ 9 ni h セ in manhattan. 鵞の 30 erai 90 ー ms are & bO セ セ 0 90 セセ . 製・一 1 : 工 wish 1 sa 響セ h & を一 Poor Ma れ ha しセ日 100k3 、、ご芋・・ミ Ck. Nei1 joined the chat 、当↑・臧 : Xyie, 工 ju 島セ wa セ 0h04 第 your 90 て m die ・ Satellite , Fiatiton 1 け 下 e Ne 第、 e 引 V ⅱは 0 総 / を イ P_S ⅸ Cgty SQ いれ、 こ : QV を Park S 曲績い S を碑 ) 0 聳材いま ー曰會日な、卩 - ーⅱ V 第 ! もを . ニ。 Figure 10-15. Viewing ビ co 川〃に d co 〃な司記厩夜・ Adding a chat based on long polling can be done from almost any server setup; if it's not directly supported, firing up a simple Tornado web server on a separate port will certainly dO the trick. The SMS integration on this application is a small extra feature— the application would certainly work without it—but having it there enhances the user's experience. Likewise for lnstant Messaging, this addition can easily be done by firing up an App Engine instance and responding to a few API calls. Having these technologies ⅲ your application is pretty easy—small steps, but the end result is that users can interact with your application in drastically different ways. AS the web increasingly moves away from the desktop computer, it'S important that your application can respond in every way your users expect. Depending on your applica- tion, there is a good chance that users would start interacting with your application more frequently, and hang around longer, if you gave them the means to do it on their own terms. Ge 謝 S 第部 JO 爲 Qn 、 に 9 卩・ Elmer m を第 ′ Bo し 0 00502 0m0 区Ⅲ 5 0 し をを stR を朝 East 望亜 Park 1W0 Bridges 。 ' ' ふ怺し第 - Ⅲ技新朝い 478 296 ー Chapter 10 : Putting lt 則 Together
on the Web. These identifiers must be unique across all of App Engine, so check the availability and pick something suitable. If you want to write a new application in the future, you can always create a second application and give it a new identifier. You're also asked t0 provide a title for the application. This title will be used for the screen shown tO users when they're trying tO sign intO your service. Figure 7-2 shows my attempt at filling out this form. C008 に app engine Create an Application AppIication 旧 ent ⅲ : •appspot.com ( : 區亟 ) instant-messaging You can map this app ⅱ ca 石ロ 0 yo•新 0 ldoma 示ね t . 、皇旦 . App ⅱ 0 酣 0 れ Title : lnstant Messaging Example osp*ayedwhen users 30C0 覊 YO 誕 app a も on 、 A hen ca 0 れ Op 02 (Advanced): 旦皿 . 但 ね市面合n@gmaに。。m ー山 . 凵匯 S ! 胆理 ! G009 App Engine 0 ⅵ d an APifOt 山 00 朝 02m yo い緡 0 僑 . lfyou 朝 00S0 notto い 0 Ⅲ anyone 市物研協朝目 be 0 YO い : 飜の n. H(B ・既 ifyou け協しれき . 創 need め specify nowwhO c 謝 1 引ⅱ n yo 山 ap がに i< 第 : 0 n 訓 G009 Accoun users (default) け yo 罅 app*ication u を 0 2 し en a 社 on 、 anyono wtth 謝池 G009 A300n い第 ay 引ⅱル (Th1S ⅱ託 de & Gmail Accoun 、 bu をま s •not• inciude ccoun on any GOI ) 0 に Apps domains-) Cancel Save 日 g ″尾 7-2. CO れ g 肝ⅲ g ) 0 肝 Goog App Engine app わ〃 Creating an AppIication with the SDK TO develop applications on Google's cloud, you must first install the GoogIe App En- gine SDK for Python. lt's available for Mac OSX, Windows, and Linux from the App Engine project on Google C0de ( 中 : / ん 0 . goog . co 川ん ppe れ g ⅲ e / do ル司 oads. 襯 I). DownIoad the appropriate package and install it for your platform. Both the Mac and Windows versions have a graphical interface for creating projects, launching the de- velopment server, and deploying the code t0 Google's servers. After installing the SDK, you have a program called GoogIe App Engine Launcher. Using that application, create a "New Application" from the File menu. lt prompts you for an 、 'Application Name" and a directory t0 house it in. The Application Name should match the Application ldentifier that you selected when creating your application on the Google website. Figure 7-3 should resemble what you see. Getting Started with Goog 厄 App Engine ー 1 引
! 5 ted ー 0 れ e obile Activate ド 0b00k Texts ( 5 p 1 of 3 ) P 厄 as に ( h005 に your country and mobile 5 にⅣにに 0 ⅵ de 「 below.* 〔 4 」」」こ」」」」こ : 」イ Choose a carrier 引ま yo リ「 mo を厄 ( を町 ov 電を「 AT&T Verizon Sprint Nextel B00 式 Virgin USA Helio Cellular South T-Mobile CeIIcom リ 0P0 广 them this 朝 N に ーれ 0 ー e COA iOn ( 0 e. 式 ( h 「 92 fO 「 this 5 を「朝 ( 色 5 盟 nd ~ 電 me 3 M を 55a905 Figure 8-7. Entering ツ 0 ″ r ル汁 el い s c のⅵげ 0 れ朝 c 訪 00 た e.dlAT&T 3G Ca Messages 8 : PM 1010100018 CanceI Edit Add to Contacts D00 19 , 2009 8 : 21 PM FRM:troden@neIson.Ioca I(TedRoden) SUBJ:Greetings MSG:SMS from the command line 0 日 g 尾 8-2. The emailed 川お g ビ 0 〃 a 〃わ 0 〃に おト d The 5M5 Landscape ー 155
嶬で b Design & Development Building the Realtime User Experience The 嶬で b is increasingly happening in realtime. 嶬宝 h websites such as Facebook and Twitter leading the way' users ℃ coming tO expect that Ⅱ sites should serve content as it occurs¯on smartphones as well as computers. This bOOk shows you h ( ) 从 to build realtime user experiences by adding chat' streaming content' and including at 紅 without making big changes tO the existing infrastructure. You'll alSO learn hOW tO serve realtime content beyond the browser. Throughout the b00k are many practical JavaScript and Python examples that you can use on your site 〃 0 . And in the final chapter, you'll build a location-aware game that combines all of the technologies discussed. Use the latest realtime syndication technologY' including PubSubHubbub BuiId dynamicwidgets on your homepageto show realtime updates from sources ■ Learn h0Wt0 use long polling t0 push" contentfrom your server tO browsers create an application using theTornado web serverthat makes sense Of massive amounts Of streaming content Understand the unique requirements for setting up a basic Chat service Use IM and SMS tO enable usersto interactwith your site outside Ofa web browser lmplementcustom analyticsto measure engagement in realtime ■ ■ 第 ■ Previous experience developing web iS recommended. 。 7 〃な 0 〃召グ〃 川尸阨 / ん〃 0 〃召 〃〃肥わ . 爿叩ド C() 〃 7 わ加 es 4 〃 S 2 / 72 / 〃 鰓〃 / ⅳ〃 . 鹿召リ功 / 〃 g 花ノ es な / 加″ g カ川を昭 / 0 励 0 間″側励 c 砠ツ / 娵ル召 ア / / / ツ翫励 / 加雇 —MarshaIl Kirkpatrick 朝一加ら ReadWriteWeb Ted Roden is a member 0f the Research and Development group at 7 カ e Ne 肥 7 カ〃い , wher ℃ he has extensively 1 ℃ searched topics related tO realtime user experience. 0 REILLY 0 0 「 e Ⅲ y. ( om US $ 34.99 CAN $ 43.99 I S B N : 9 7 8 ー 0 ー 5 9 6 ー 8 0 6 1 5 ー 6 5 3 4 9 9 川ⅧⅢはⅢⅧ馴 II 9 / 8 0 5 9 6 8 0 6 1 5 6 Free online edition fo 「 45 days with purchase of this bOOk. Details on last page. Safarl BooksOnline
poly. push(new GLatLng(Cy,Cx)) / / add the first point to complete the circle poly. push(p01y[0]) ・ = new GP01ygon(p01y, ( 010r , 3 , var 1ine 1 , ( 010r , 0.2 ) ; ipGeo. map. add()verlay(line) 9 : 13 PM 調 Carrier 9 : 14 PM 田 3 0 を 01 協 0000 調 Carrier ーれざ em ー 000 { 第 0 ca 旧 os 8091 / iPandemic localhost:8091 / -[c10seJ S セ【 & えれ : YS い 38 第師・ t : をを 5 之・ exa に 1 を .00m n . し 00P WEREO 00 に ん面ね 0 10- い ! Y をに - 十一口丁 日 g Ⅲ℃ 10-4. Viewing に〃ビル co ″ g わ川韲たげⅢ a 靃ⅲ 0 〃叡ⅲ角 r 襯酊〃 This code generates a circle and displays it on the map based on a single center point and a radius Of a certain number Of miles. Essentially, it figures out hOW tO draw the circle based on hOW many POintS Ⅵ℃ elect tO ShO 、 V. Because ShO 、 this on a mobile phone, and performance counts in realtime applications, we re only going tO ShOW l() points, but this can be increased [ 0 be as smooth as you'd like. Once again, this COde was not written by me from scratch; it is a modified function based on the work ofJeremy Schneider ( 印 : 巧げ s 訪〃 e 畆わ log 平可 . co 川 / 2008 月 2 / 朝れ〃 - ル - 記 ra ル - 朝℃いⅲ - goog に . 襯 l). Building the Game ltself ー 249
With this widget finished, all of the widgets should be complete. Open your browser to index. わ t t0 see all 0f the widgets in action. Figure 3-3 should resemble the finished product. The Live lmages widget can be entertaining and addictive tO watch, but keep in mind this iS an unfiltered view Of images that are posted [ 0 TWitter. Occasionally this widget may contain images that are not safe for work. index. htmi ' ①い十 : れに / / / し / び od に n / D 「 opbox / book - ( od れ om ~ page ハ nd を x. ⅸ m ー F 「朝 F Upd い : 3 new 「い Trending SinkingRaindrop: @tRmyveins michael a 「「ⅲ on : InvestimonialsWants 嶬なれ a second. に you seeing To Be YourGuide To QuaIity Financial Products - TwiIight New Moon'? れ叩 : ww ・いⅧ朝川 : 叩 9 。 Janie_t: @minmizeMimi we ' going い see new moon this weekend kamla bhatt: Ftesh い n 盟 ( に、 from 信 rn パ and we're gomg い have a sleepoverlol!!!! tnalket make a big differnce ⅲ fixing pasta sauce. rawrbbyrawr: HAHAHA! い aw NEW M ( ) 0 、 again tcxiay! く 333 kamla bhatt: PBS is showing Secrtsofthe ) い ! YFS! D 供記 on 26 / Ⅱⅲ Mumbai. Talks 3 ⅸ the 皿 k , ( on 、 un 一に ( h and 面 media. tiffany: yesi didjustmake a # hand 田水ら : 1 似Ⅱ noisy me 「お . Testing Pentax K7 sharpness with firmware L02 ・ 、。 - 加〃 www 」興朝ト ,Y 眠「時印 m / 2 Ⅸ ) 9 … e い第 Q , し 0 四に Live 川 g 社せ a 「い山冊 laaaa ル New moon sampe 3 btoskop. G 代靆 ! 「 a 山い h ⅳ加 watched New M 供川 with my fnend 代 n marzialuna: Otro con problemas. RT @EzioOliva: Hoy fui alesuenode 日 g Ⅲ℃ 3-3. AII 尾 e widgets ⅲ行 0 〃 Was AIIa Setup! These examples used a simple Ajax-like implementation tO constantly ping the server for new updates. ln this case, it used the best tOOl for thejOb [ 0 provide constant updates from various places around the Web. However, watching this example come together, you've probably noticed a couple 0f things. The first problem with this approach is that although content may be updated quickly, it would be a stretch tO call this a "realtime" user experience. AII we're dOing here is checking for new updates, displaying the updates, waiting for a while, and then re- peating the process all over again. What happens when an update is ready on the server while our script it JtlSt waiting in the timeout loop? NO matter how fast we speed up thattimeout, there is still going to be a delay. Figure 3-4 shows the messages queuing up while our COde waits in the timeout IOOP. 54 ー朝叩 te 「 3 : The Dynamic Homepage (Widgets in Pseudorealtime)
After signing up for the service and registering a keyword, you'll still need t0 create an API key to use the API functions. This is a simple process initiated from the developer section on the TextMarks website ( 印 : 〃ルルル . x la s. co 川 / ツ 4 / 尾 g 庄 You'll be greeted with a form resembling Figure 8-6. After filling outthis form, you should receive an email message from the service almost immediately with your API key. Save this key in a safe place; it'll be used when we start writing the messaging functionality. Registration Form FiII に form t*low for each a 叩ⅱⅱ intend 協 integrate with けに TextMarls V2 API. Your Web Si : http://therealtimebook.com URL ⅸ y 似 web 、い , ℃ 01 半田 IY. E-mail: tedroden@gmail.com A 朝 1 朝 1 い m , for 登 p 「 0 ℃ 0 & 5 朝 app め化 lnte 社 Use: My u “ ge. ・・ D 合 sc 「の 0 t め物 you ⅲ t 田 tO 0 0 API, ex 0 にづ traffc, 飜 c. Ylread & ag 「 to Tex ひぬⅸ sT msof rvi ( 色 REGISTER 日 g Ⅲ℃ 8-6. A 叫″ⅲれ g の 1 ハ円た 0 〃 T こ M の・ The Python class Having built the SMSService interface class, we can start building the actual service classes by expanding on the class we have already defined. ln this case, we'll create a class called Textmark. TO handle sending messages from the TextMarks service, we only need t0 override two methods. lnside your s 川 s. file, add the following code: class Textmark(SMSService) : def init_(self) : # The authentication parameters supplied by textmarks self. auth user = yo リて -0 リ t わ - user ' self. auth pass = yo - 側 t わ - P055 ・ # ( a11 the constructor Of the parent class init_(self, SMSService. 141r # textmarks. (0mな short code ノ 0 - 々 ey Ⅳ 0 て d ・ , api key= ・ Y0礪-0廖-keノ 168 ー Ch 叩 ter 8 : SMS
user types two keys in a row, chances are good that it'll be within that 1.5 second window, and we won t send a notification tO the server. We're not sending a request with every keystroke, because that would be a ton of keystrokes and thus a 10 [ 0f HTTP requests. We can easily determine whether a user is typing and still conserve HTTP requests by giving it a reasonable timeout between keystrokes. Figure 6-5 shows this process. User Keystrokes . seconds . ( ondS First keystro ke Th e last keystro ke more than 1.5 seconds ago Ping/typing on the server Figure 6-5. Waiting 7.5 co 〃ホわ et ル〃尾甲ィなの尾〃 0 なれ e ルた eys ℃たお We're effectively queueing up and sending notifications when one user starts typing to another user. All that's left is [ 0 inform the user on the receiving end. Add the following code into the chat. handle updates method of 訪 : if(message ・ type = ・ login' ) { 'typing' ) { else if(message ・ type / / get the user var u = chat. users[message. data. from user id]; u. panel. setFooter(). user name + ・ is typing ・ / / clear any existing timeouts clearTimeout(chat. timeouts[u. user id]); / / setup a new timeout chat. timeouts[ u. user id] = setTimeout(function() { u. pane1. setFooter(). user_name 十 } , 300 の ; chat. P011 ( ) ; / / keep polling typed ・ Acting Natural ー 125
client-side work in this example. However, let's set up the basic Tornado server file SO that we can get this example running. The Python SheII Since this example alSO uses Tornado, there iS no complicated server setup. 、、み e simply write a script using the right libraries and run it directly. Open up a file called 訪ト 既 and add the following code: import os, logging import tornado. httpserver import tornado. i0100P import tornado. web import uuid import simplejson as json from tornado. options import define, options # Define options that can be changed as we run this via the command line defau1t=8888, he1p="Run server on a specific port", type=int) define("port" # a convenience function tO quickly build a notification Object data} # Dec1are the chat class . We' 11 fill it out later type, ・ data ' ・ return { 'type' def notification( type, _data) : class Chat(0bject) : paSS # The Basic handler class class BaseHand1er(tornado. web. RequestHand1er) : @property def chat(self) : return self. application. chat # handle the main html request class MainHand1er(BaseHand1er) : def get(self) : self. render("templates/chat-main. html' # Extend tornado' s web. Application module class App1ication(tornad0. web. Application) : def init_(self) : # setup the URL handlers handlers (r"/", MainHand1er), # setup the static path path = os. path. join(os. path. dirname( settings = dict(static path=path) tornado. web. App1ication. init_(self, handlers, file_) , "static") **settings) Setting Up the Basic C0de ー 103
g ア川〃 . / 山 ) t0 generate a new API key for this application. When the form asks for your URL, enter the App Engine URL created earlier. The next page will display your unique Google Maps API key; be sure t0 COPY that URL and save it locally on your computer. we'll be using that key ⅲ this application, and there is no obvious way t0 get Google Maps t0 display the same key for you again ・ EC2 0 「 0ther Hosted Server part 0f this application runs on Google's App Engine infrastructure' but another part is hOSted on a standard web server. ThiS server needs [ 0 have python installed and you must have shell access, as we'll be running this application with the Tornado which must be started from the command line. Any public-facing server will work for this task as long as you have shell access and python is either installed or installable. If you re using this server tO hOSt another application, you can easily hOSt this right alongside it using a different port. If you're using Amazon EC2 t0 hostthe part 0f the application that does not run on App Engine, you're going t0 want t0 open a new port for this application. TO d0 that from your EC2 instance, you'll wantto open the port with the following command: $ ec2-authorize default -C ~ / ノ 0 イ t - le. pem -K *. pem \ -p 8088 Many of the community AMls (Amazon Machine lnstance) already have the ec2- authorize command installed. If you don't have it installed already, your package manager may have it under ec2-api-tools; otherwise, it can be downloaded directly from the Amazon 从 b services developer website ( わな〃 : 〃 de リ elO 〃夜・ . a 川 a て 0 〃ル e わ覊なお . co 川ん 0 〃〃厩リ〃川 a ″ D = 357 c ego リ ID = 88 ). GeoModel This application deals with geolocation in a number 0f different ways. As users cough and germs spread, going tO need tO determine hOW close one germ iS tO another. There are a couple Of ways tO dO this, but a convenient methOd would be tO ask our datastore [ 0 return a list Of germs within a specified distance Of a certain germ. While App Engine's data store does understand the concept 0f latitude and longitude coor- dinates through its Geopt datatype, it has no built-in ability tO return a list based on proximity. TO get this functionality, we can use an open source third-party library called GeoModeI ( 印 : / ん 0 . goog . co 川ゆ / geo 襯 ode の . The easiest way [ 0 incorporate the source iS tO check out the latest version from Subversion directly intO our directory. $ svn ( 0 http://geomodel.googlecode.com/svn/trunk/geo geo/geocell test. py A geo/geocell. py A geo/geomodel. py A 220 ー Ch 叩 ter 10 : Putting lt 則 Together