Building a chat application with JavaScript MVC

Building a chat application is an exciting project that allows users to communicate in real-time. In this blog post, we will explore how to build a chat application using JavaScript MVC (Model-View-Controller) architecture.

What is JavaScript MVC?

JavaScript MVC is a design pattern that separates the application into three main components: Model, View, and Controller.

Setting Up the Project

To start building our chat application, we need to set up the project structure. Create a new directory and navigate to it in your terminal. Then, follow these steps:

mkdir chat-app cd chat-app

Next, initialize a new npm project by running the following command:

npm init -y

This will generate a package.json file that holds information about our project.

Installing Dependencies

For our chat application, we will be using the following dependencies:

You can install these dependencies by executing the following command:

npm install express socket.io

Implementing the MVC Structure

Now that we have our project set up and dependencies installed, let’s implement the MVC structure for our chat application.

Model

In the models directory, create a new file called chatModel.js. This file will contain the logic for managing the chat messages and user information.

Example code:

class ChatModel {
    constructor() {
        this.messages = [];
    }

    addMessage(message) {
        this.messages.push(message);
    }

    getMessages() {
        return this.messages;
    }
}

module.exports = ChatModel;

View

In the views directory, create a new file called chatView.js. This file will handle rendering the chat interface and managing user interactions.

Example code:

class ChatView {
    constructor() {
        this.messageContainer = document.querySelector('#message-container');
        this.messageInput = document.querySelector('#message-input');
        this.sendMessageButton = document.querySelector('#send-message-button');
    }

    renderMessage(message) {
        const messageElement = document.createElement('div');
        messageElement.innerText = message;
        this.messageContainer.appendChild(messageElement);
    }

    getMessageInput() {
        return this.messageInput.value;
    }

    clearMessageInput() {
        this.messageInput.value = '';
    }

    addMessageListener(callback) {
        this.sendMessageButton.addEventListener('click', () => {
            callback();
        });
    }
}

module.exports = ChatView;

Controller

In the controllers directory, create a new file called chatController.js. This file will handle interactions between the model and the view.

Example code:

class ChatController {
    constructor(model, view) {
        this.model = model;
        this.view = view;

        this.view.addMessageListener(() => {
            const message = this.view.getMessageInput();
            if (message.trim() !== '') {
                this.model.addMessage(message);
                this.view.clearMessageInput();
            }
        });

        this.model.messages.forEach(message => {
            this.view.renderMessage(message);
        });
    }
}

module.exports = ChatController;

Integrating Socket.IO

To enable real-time communication in our chat application, we will integrate Socket.IO with our MVC structure.

In the main index.js file, add the following code:

const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);

const ChatModel = require('./models/chatModel');
const ChatView = require('./views/chatView');
const ChatController = require('./controllers/chatController');

const model = new ChatModel();
const view = new ChatView();
const controller = new ChatController(model, view);

io.on('connection', socket => {
    socket.on('message', message => {
        model.addMessage(message);
        socket.broadcast.emit('message', message);
    });
});

app.use(express.static('public'));

http.listen(3000, () => {
    console.log('Server started on http://localhost:3000');
});

Deploying the Chat Application

To deploy the chat application, follow these steps:

Conclusion

By following the MVC architecture and integrating Socket.IO, we have successfully built a chat application using JavaScript. This architecture allows for a clean separation of concerns and easy maintenance of the application. Happy chatting!

#chat #javascript