CAPÍTULO 1. LA CULTURA PROFESIONAL DOCENTE
1.3. Cultura profesional y profesionalidad docente
We will set up a separate project for this chapter, demonstrating how we can create a simple chat application that demonstrates the essentials of using WebSocket.
Create a separate project folder and name it chat-app. In this folder, create a blank file named app.js. Finally, open your terminal or the command prompt, go into the folder, and run the following:
npm init
Answer the questions prompted by Node.js and make sure that you specify app.js as the entry point for the application.
Installing socket.io
We will install socket.io, as always, by using our good friend npm. From your terminal, issue the following command:
npm install socket.io
That's it. We are now good to go. Let's start setting up our server! However, before we do that, let's start from the top and define a basic chat interface for us to play with.
Creating a chat interface
We are not creating the next WhatsApp (yet!). So, building a full-fledged chat interface is a bit beyond what we want to achieve in this chapter. Let's go for something basic, as illustrated in the next screenshot:
To create this layout, create the index.html file in your project folder and insert a basic HTML setup inside it, as follows:
<!DOCTYPE html> <html>
<head >
<meta charset="UTF-8">
<title>Socket.IO chat application</title> </head>
<body> </body> </html>
We will now add some custom elements to this markup in order to get the layout we need for our chat to be nice and user friendly. First, import the Bootstrap CSS framework by inserting a link into href in the header:
<head lang="en">
<meta charset="UTF-8">
<title>Socket.IO chat application</title> <link rel="stylesheet"
href="http://maxcdn.bootstrapcdn.com/bootstrap/ 3.3.4/css/bootstrap.min.css"/>
</head>
Bootstrap, originally developed by Twitter, is a widely used framework that can be utilized to quickly build responsive web interfaces. As web design is beyond the scope of this book, we will use it in order to keep manual styling to a minimum. Don't worry if you are unfamiliar with the framework. It is very intuitive, and we will explain what you need to know along the way.
Next, let's add a Bootstrap container div to our interface, as follows:
<body>
<div class="container"></div> </body>
This is simply an organizational unit that Bootstrap uses to contain a set of UI elements inside a container so that the layout fits well on the screen being used. Next, inside the container, let's add a chat-box, as follows:
<div class="row">
<div id="chat-box" class="well">
<ul id="chat-view" class="list-unstyled"></ul> </div>
The following are the three classes that are being used in the preceding code: • The row class, which is similar to the container class, is an organizational
unit that confines the elements that it holds to a single row in the layout. • The well class, which creates a shaded container, make the elements it
contains more visually distinct.
• The list-unstyled class, which simplifies the ordinary HTML unordered list tag, removes, among other things, the bullet styling that appears next to elements.
The end result is shown in the following screenshot:
Now, let's add the elements needed for users to enter their names and submit actual messages, as follows: <form action=""> <div class="row"> <input type="text" id="chat-name" class="form-control" placeholder="Your name"> </div> <div class="row"> <input type="text" id="chat-message" class="form-control" placeholder="Enter message"> <button id="chat-submit" type="submit" class="btn btn-default">Send </button> </div> </form>
By now, you should be familiar with most of the UI elements and what they do, and the rest is nothing but a standard HTML form (note that we do not provide an action for the form itself; submissions will be handled dynamically through JavaScript instead). Note that we added some classes to the form elements. These are standard Bootstrap layout classes that are used to style the appearance of the elements themselves. They do not introduce any functionality in themselves, and
That's it! If you open the file in your browser, you will see the following:
The chat obviously does not really do anything at the present time. We will do something about this in a moment, but first, let's see how we can serve the HTML file that we just created directly from Node.js.