Redis Cookbook by Macedo Tiago Oliveira Fred

Redis Cookbook by Macedo Tiago Oliveira Fred

Author:Macedo, Tiago, Oliveira, Fred [Tiago Macedo and Fred Oliveira]
Language: eng
Format: epub
Tags: COMPUTERS / General
ISBN: 9781449315412
Publisher: O'Reilly Media
Published: 2011-07-24T16:00:00+00:00


Implementing the client side code

With the server side part completed, all we have to do is create a small page that connects to Node.js, sets up Socket.IO on the client side, and then deals with incoming and outgoing messages. So let’s create a page like that now. Here’s the main trunk for a very simple HTML5 page:

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Chat with Redis</title> </head> <body> <ul id="messages"> <!-- chat messages go here --> </ul> </body> </html>

Now we need to include the two main pieces we need to get the functionality working: jQuery and Socket.IO. We’ll grab the first from Google’s CDN, and the second from our Node.js server (Socket.IO takes care of setting this up for you automatically). Insert these two lines in the head section of your page:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <script src="http://localhost:8000/socket.io/socket.io.js"></script>

We’re now ready to connect to Node.js from this page and start listening to and processing messages. Add the following code to your head section:

<script> var socket = io.connect('localhost', { port: 8000 }); socket.on('message', function(data){ var li = new Element('li').insert(data); $('messages').insert({top: li}); } </script>

This piece of JavaScript makes the client-side Socket.IO connect to our Node.js instance on port 8000 and start listening to message events. When a message arrives, it creates a new list element and adds it to the unordered list we had already added to our code. Remember, this is very simple code and the resulting chat page will look ugly by default, but it is also trivial to update it to look better.

All we’re missing at this point is a form and a way to send messages from one client to the server so that they can be broadcast to everyone else. This is done with Socket.IO’s emit function, which we already used on the server side as well. Write something like:

<form id="chatform" action=""> <input id="chattext" type="text" value="" /> <input type="submit" value="Send" /> </form> <script> $('#chatform').submit(function() { socket.emit('message', $('chattext').val()); $('chattext').val(""); // cleanup the field return false; }); </script>

When a client fills the form and clicks Send, jQuery will use our socket variable to emit a message event to the server, which will then broadcast the message to everyone else. The return false statement in the last script tag keeps the form from actually being submitted. Our submission code is handled by Socket.IO.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56085)
What's Done in Darkness by Kayla Perrin(26619)
The Fifty Shades Trilogy & Grey by E L James(19100)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19083)
Shot Through the Heart by Mercy Celeste(18955)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17139)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17026)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16900)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16840)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16470)
The Subtle Art of Not Giving a F*ck by Mark Manson(14384)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14158)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13678)
Scorched Earth by Nick Kyme(12788)
Drei Generationen auf dem Jakobsweg by Stein Pia(10984)
Suna by Ziefle Pia(10903)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(10573)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(10552)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(10522)
Scythe by Neal Shusterman(10370)