9.5. Plain-Text Message![]() Acknowlegement, Custom, PlainText, Semantic, String Figure 9-6. Plain-Text Message![]() 9.5.1. Developer StoryDevi's working on an auto-completion form field. Every few seconds, the server needs to respond with a list of suggestions. Devi codes the server side to output the suggestions as a comma-separated list, as it's convenient to output on the server and easy to parse in the browser. 9.5.2. ProblemHow can you transfer data between server and browser? 9.5.3. Forces
9.5.4. SolutionPass simple messages between server and browser in plain-text format. This is a broad pattern, because "plain-text" is an ambiguous term. The emphasis here is on keeping things simpleXML or JSON are often more appropriate for complex data structures, but can also add unnecessary complexity. Despite the name, XMLHttpRequest is perfectly capable of working with plain-text responses. As discussed in XMLHttpRequest Call (Chapter 6) and XML Message (later), the browser script can decide how to interpret the response in XMLHttpRequest by switching between the responseText and responseXML properties. Plain-text formats include:
While browser support for XML is reasonably good, especially with third-party libraries, it's also quite easy to manipulate arbitrary text formats. JavaScript provides a standard string manipulation library, including the particularly helpful regular expression and grouping facilities. The case for plain-text is particularly compelling in the following situations:
9.5.5. Real-World Examples9.5.5.1. Lace ChatBrett Stimmerman's Lace Chat (http://www.socket7.net/lace/) is an Ajax chat app (Figure 9-7). When the page is initially shown, all user messages are downloaded as a single, long string. Within the string, each user message is captured as a set of attributes, separated by pipe characters: 1135646360:749||||date-1135646204||*Monday, 26 December 2005||||hour-17||*17:00||||1135646204||Posted about 3 minutes ago at 17:16||Aj Two||Yeah you can *SO* chat in real time||||1135646212||Posted about 2 minutes, 52 seconds ago at 17:16||*Lace||AJ One has joined the conversation||||1135646212|| Figure 9-7. Lace Chat![]() 9.5.5.2. Magnetic PoetryIn Magnetic Poetry (http://www.broken-notebook.com/magnetic/), you drag tiles around a workspace. When the browser receives an updated position, the message looks like a structured command statement: Update magnetic_poetry set top_value=291, left_value=119 WHERE id=81 9.5.5.3. HousingMapsHousingMaps (http://housingmaps.com) is a mashup between Google Maps and Craigslist (http://craigslist.com), a community-oriented classified advertising web site. The HousingMaps portion is downloaded with an XMLHttpRequest Call that retrieves the 15 or so results matching a query. Each result represents a single home and is broken into a sequence of attributes, one line at a time, and prefixed with an identifier: PA:34.0917 PN:-118.28 IC:p DS:One Bath With Small Loft Duplex Near Sunset Junction AD:Hyperion Ave &&& Sunset Blvd</line><line> Los Angeles</line><line>CA ... I4:a.im.craigslist.org/rR/iv/X1dT18TzV07cfjuQzouhKQ5EETuK.jpg LC:1071999 PA:34.0763 PN:-118.294 IC:p DS:1930&'s Restored Spanish Beauty- 1St Month Free AD:Berendo &&& Beverly</line><line> Los Angeles</line><line>CA ... I4:a.im.craigslist.org/n0/Xm/SS3hSoIWLOsc8wOjaiLGQK5HFbzl.jpg LC:1072004 ... 9.5.6. Code Example: Lace ChatThe response in Lace Chat is a list of attributes, separated by pipe characters. Each complete record is separated by ||||, and each attribute within a record is separated by a smaller string, ||. The handling code first splits the string into an array of records, then splits out the attributes within each record: // Records are separated by four pipes |||| var results = this.httpGetObj.responseText.split('||||'); ... for (var i = 1; i < results.length; i++) { var first; // Fields are separated by two pipes || var fields = results[i].split('||'); ... var timeStr = fields[1]; var textStr = fields[3]; ... p.setAttribute('id', 'msg_' + fields[0]); ... } 9.5.7. Alternatives9.5.7.1. XML MessageAs discussed in the "Solution," an XML Message may be a better alternative for more complex data. |