Building a frontend =================== As we have seen in the :doc:`../basics/readme`, Feathers works great in the browser and comes with :doc:`../basics/clients` that allow to easily connect to a Feathers server. In this chapter we will create a real-time chat frontend with signup and login using modern plain JavaScript. As with the :doc:`../basics/readme`, it will only work in the latest versions of Chrome, Firefox, Safari and Edge since we won’t be using a transpiler like `Babel `_. The final version can be found `here `_. .. note:: We will not be using a frontend framework so we can focus on what Feathers is all about. Feathers is framework agnostic and can be used with any frontend framework like React, VueJS or Angular. For more information see the :doc:`../frameworks/readme`. Set up the index page --------------------- We are already serving the static files in the ``public`` folder and have a placeholder page in ``public/index.html``. Let’s update it to the following: .. code:: html Vanilla JavaScript Feathers Chat
This will load our chat CSS style, add a container div ``#app`` and load several libraries: - The :doc:`../basics/clients` (since we are not using a module loader like Webpack or Browserify) - Socket.io provided by the chat API - `MomentJS `_ to format dates - An ``app.js`` for our code to live in Let’s create ``public/app.js`` where all the following code will live (with each code sample added to the end of that file). Connect to the API ------------------ We’ll start with the most important thing first, the connection to our Feathers API. We already learned how Feathers can be used on the client in the :doc:`../basics/readme`. Here, we do pretty much the same thing: Establish a Socket connection and initialize a new Feathers application. We also set up the authentication client for later: .. code:: js // Establish a Socket.io connection const socket = io(); // Initialize our Feathers client application through Socket.io // with hooks and authentication. const client = feathers(); client.configure(feathers.socketio(socket)); // Use localStorage to store our login token client.configure(feathers.authentication({ storage: window.localStorage })); This allows us to talk to the chat API through websockets, for real-time updates. Base and user/message list HTML ------------------------------- Next, we have to define some static and dynamic HTML that we can insert into the page when we want to show the login page (which also doubles as the signup page) and the actual chat interface: .. code:: js // Login screen const loginHTML = `

Log in or signup

`; // Chat base HTML (without user list and messages) const chatHTML = `
Chat
`; // Add a new user to the list const addUser = user => { const userList = document.querySelector('.user-list'); if(userList) { // Add the user to the list userList.insertAdjacentHTML('beforeend', `
  • ${user.email}
  • `); // Update the number of users const userCount = document.querySelectorAll('.user-list li').length; document.querySelector('.online-count').innerHTML = userCount; } }; // Renders a new message and finds the user that belongs to the message const addMessage = message => { // Find the user belonging to this message or use the anonymous user if not found const { user = {} } = message; const chat = document.querySelector('.chat'); // Escape HTML const text = message.text .replace(/&/g, '&') .replace(//g, '>'); if(chat) { chat.insertAdjacentHTML( 'beforeend', `
    ${user.email}

    ${user.email} ${moment(message.createdAt).format('MMM Do, hh:mm:ss')}

    ${text}

    `); chat.scrollTop = chat.scrollHeight - chat.clientHeight; } }; This will add the following variables and functions: - ``loginHTML`` contains some static HTML for the login/signup page - ``chatHTML`` contains the main chat page content (once a user is logged in) - ``addUser(user)`` is a function to add a new user to the user list on the left - ``addMessage(message)`` is a function to add a new message to the list. It will also make sure that we always scroll to the bottom of the message list as messages get added Displaying the login/signup or chat page ---------------------------------------- Next, we’ll add two functions to display the login and chat page, where we’ll also add a list of the 25 newest chat messages and the registered users. .. code:: js // Show the login page const showLogin = (error = {}) => { if(document.querySelectorAll('.login').length) { document.querySelector('.heading').insertAdjacentHTML('beforeend', `

    There was an error: ${error.message}

    `); } else { document.getElementById('app').innerHTML = loginHTML; } }; // Shows the chat page const showChat = async () => { document.getElementById('app').innerHTML = chatHTML; // Find the latest 25 messages. They will come with the newest first // which is why we have to reverse before adding them const messages = await client.service('messages').find({ query: { $sort: { createdAt: -1 }, $limit: 25 } }); // We want to show the newest message last messages.data.reverse().forEach(addMessage); // Find all users const users = await client.service('users').find(); users.data.forEach(addUser); }; - ``showLogin(error)`` will either show the content of loginHTML or, if the login page is already showing, add an error message. This will happen when you try to log in with invalid credentials or sign up with a user that already exists. - ``showChat()`` does several things. First, we add the static chatHTML to the page. Then we get the latest 25 messages from the messages Feathers service (this is the same as the ``/messages`` endpoint of our chat API) using the Feathers query syntax. Since the list will come back with the newest message first, we need to reverse the data. Then we add each message by calling our ``addMessage`` function so that it looks like a chat app should — with old messages getting older as you scroll up. After that we get a list of all registered users to show them in the sidebar by calling addUser. Login and signup ---------------- Alright. Now we can show the login page (including an error message when something goes wrong) and if we are logged in call the ``showChat`` we defined above. We’ve built out the UI, now we have to add the functionality to actually allow people to sign up, log in and also log out. .. code:: js // Retrieve email/password object from the login/signup page const getCredentials = () => { const user = { email: document.querySelector('[name="email"]').value, password: document.querySelector('[name="password"]').value }; return user; }; // Log in either using the given email/password or the token from storage const login = async credentials => { try { if(!credentials) { // Try to authenticate using the JWT from localStorage await client.authenticate(); } else { // If we get login information, add the strategy we want to use for login const payload = Object.assign({ strategy: 'local' }, credentials); await client.authenticate(payload); } // If successful, show the chat page showChat(); } catch(error) { // If we got an error, show the login page showLogin(error); } }; document.addEventListener('click', async ev => { switch(ev.target.id) { case 'signup': { // For signup, create a new user and then log them in const credentials = getCredentials(); // First create the user await client.service('users').create(credentials); // If successful log them in await login(credentials); break; } case 'login': { const user = getCredentials(); await login(user); break; } case 'logout': { await client.logout(); document.getElementById('app').innerHTML = loginHTML; break; } } }); - ``getCredentials()`` gets us the values of the username (email) and password fields from the login/signup page to be used directly with Feathers authentication. - ``login(credentials)`` will either authenticate the credentials returned by getCredentials against our Feathers API using the local authentication strategy (e.g. username and password) or, if no credentials are given, try and use the JWT stored in localStorage. This will try and get the JWT from localStorage first where it is put automatically once you log in successfully so that we don’t have to log in every time we visit the chat. Only if that doesn’t work it will show the login page. Finally, if the login was successful it will show the chat page. - We also added click event listeners for three buttons. ``#login`` will get the credentials and just log in with those. Clicking ``#signup`` will signup and log in at the same time. It will first create a new user on our API and then log in with that same user information. Finally, ``#logout`` will forget the JWT and then show the login page again. Real-time and sending messages ------------------------------ In the last step we will add functionality to send new message and make the user and message list update in real-time. .. code:: js document.addEventListener('submit', async ev => { if(ev.target.id === 'send-message') { // This is the message text input field const input = document.querySelector('[name="text"]'); ev.preventDefault(); // Create a new message and then clear the input field await client.service('messages').create({ text: input.value }); input.value = ''; } }); // Listen to created events and add the new message in real-time client.service('messages').on('created', addMessage); // We will also see when new users get created in real-time client.service('users').on('created', addUser); login(); - The ``#submit`` button event listener gets the message text from the input field, creates a new message on the messages service and then clears out the field. - Next, we added two ``created`` event listeners. One for ``messages`` which calls the ``addMessage`` function to add the new message to the list and one for ``users`` which adds the user to the list via ``addUser``. This is how Feathers does real-time and everything we need to do in order to get everything to update automatically. - To kick our application off, we call ``login()`` which as mentioned above will either show the chat application right away (if we signed in before and the token isn’t expired) or the login page. What’s next? ------------ That’s it. We now have a plain JavaScript real-time chat frontend with login and signup. This example demonstrates many of the core principles of how you interact with a Feathers API. If you run into an issue, remember you can find a complete working example `here `_. In the final chapter, we’ll look at :doc:`./testing`.