Creating the chat view

The chat view will be constructed from a simple form and a list of chat messages. To implement your chat view, you’ll first need to create a public/chat/views folder. In this folder, create a new file named chat.client.view.html that contains the following code snippet:

<section data-ng-controller="ChatController">
  <div data-ng-repeat="message in messages" data-ng-switch="message.type">
    <strong data-ng-switch-when='status'>
      <span data-ng-bind="message.created | date:'mediumTime'"></span>
      <span data-ng-bind="message.username"></span>
      <span data-ng-bind="message.text"></span>
    <span data-ng-switch-default>
      <span data-ng-bind="message.created | date:'mediumTime'"></span>
      <span data-ng-bind="message.username"></span>
      <span data-ng-bind="message.text"></span>
  <form ng-submit="sendMessage();">
    <input type="text" data-ng-model="messageText">
    <input type="submit">

In this view, you used the ng-repeat directive to render the messages list and the ng-switch directive to distinguish between status messages and regular messages. You also used the AngularJS date filter to properly present the message time. Finally, you finished the view with a simple form that uses the ng-submit directive to invoke the sendMessage() method. Next, you will need to add a chat route to present this view.


Adding chat routes

To present the view, you will need to add a new route for it. To do so, first create the public/chat/config folder. In this folder, create a new file named chat.client.routes.js that contains the following code snippet:

  function($routeProvider) {
    when('/chat', {
      templateUrl: 'chat/views/chat.client.view.html'

This should already be a familiar pattern, so let’s proceed to finalize the chat implementation.

Finalizing the chat implementation

To finalize your chat implementation, you will need to make a few changes in your main application page and include the client file and your new chat files. Go to the app/views/index.ejs file and make the following changes:

<!DOCTYPE html>
<html xmlns:ng="">
    <title><%= title %></title>
    <section ng-view></section>
    <script type="text/javascript">
      window.user = <%- user || 'null' %>;

    <script type="text/javascript" src="/"></script>
    <script type="text/javascript" src="/lib/angular/angular.js"></script>
    <script type="text/javascript" src="/lib/angular-route/angular-route.js"></script>
    <script type="text/javascript" src="/lib/angular-resource/angular-resource.js"></script>

    <script type="text/javascript" src="/articles/articles.client.module.js"></script>
    <script type="text/javascript" src="/articles/controllers/articles.client.controller.js"></script>
    <script type="text/javascript" src="/articles/services/articles.client.service.js"></script>
    <script type="text/javascript" src="/articles/config/articles.client.routes.js"></script>

    <script type="text/javascript" src="/example/example.client.module.js"></script>
    <script type="text/javascript" src="/example/controllers/example.client.controller.js"></script>
    <script type="text/javascript" src="/example/config/example.client.routes.js"></script>

    <script type="text/javascript" src="/users/users.client.module.js"></script>
    <script type="text/javascript" src="/users/services/authentication.client.service.js"></script>

    <script type="text/javascript" src="/chat/chat.client.module.js"></script>
    <script type="text/javascript" src="/chat/services/socket.client.service.js"></script>
    <script type="text/javascript" src="/chat/controllers/chat.client.controller.js"></script>
    <script type="text/javascript" src="/chat/config/chat.client.routes.js"></script>
    <script type="text/javascript" src="/application.js"></script>

Notice how we first added the file. It’s always a good practice to include third-party libraries before your application files. Now, you’ll need to change the public/application.js file to include your new chat module:

var mainApplicationModuleName = 'mean';

var mainApplicationModule = angular.module(mainApplicationModuleName, ['ngResource', 'ngRoute', 'users', 'example', 'articles', 'chat']);

  function($locationProvider) {

if (window.location.hash === '#_=_') window.location.hash = '#!';

angular.element(document).ready(function() {
  angular.bootstrap(document, [mainApplicationModuleName]);

To finish up your chat implementation, change your public/example/views/example.client.view.html file and add a new chat link:

<section ng-controller="ExampleController">
  <div data-ng-show="!authentication.user">
    <a href="/signup">Signup</a>
    <a href="/signin">Signin</a>
  <div data-ng-show="authentication.user">
    <h1>Hello <span data-ng-bind="authentication.user.fullName"></span></h1>
    <a href="/signout">Signout</a>
      <li><a href="/#!/chat">Chat</a></li>
      <li><a href="/#!/articles">List Articles</a></li>
      <li><a href="/#!/articles/create">Create Article</a></li>

Once you are finished with these changes, your new chat example should be ready to use. Use your command-line tool and navigate to the MEAN application’s root folder. Then, run your application by typing the following command:

$ node server

Once your application is running, open two different browsers and sign up with two different users. Then, navigate to http://localhost:3000/#!/chat and try sending chat messages between your two clients. You’ll be able to see how chat messages are being updated in real time. Your MEAN application now supports real-time communication.