Building a Simple Chat Application with Go, Gin, MongoDB, and WebSocket

gbubemi22

oritsegbubemi

Posted on July 27, 2024

Building a Simple Chat Application with Go, Gin, MongoDB, and WebSocket

In this tutorial, we will walk through building a simple chat application using Go, Gin, MongoDB, and WebSocket. We'll set up a backend server to handle HTTP requests and WebSocket connections, and manage chat messages and user interactions.

Prerequisites
. Go installed on your machine.
. MongoDB running locally or on a remote server.
. Basic understanding of Go, Gin, and MongoDB.

Project Structure
We'll organize our project into several packages:

. main: Entry point of the application.
. server: Initializes the server and routes.
. controller: Handles HTTP requests.
. middleware: Contains middleware functions.
. database: Manages MongoDB connections.
. service: Contains business logic.
. model: Defines data models.
. websocket: Manages WebSocket connections.

  1. Setting Up the Main Package Create a main.go file as the entry point of the application:

Image description

  1. Creating the Server Package In the server package, we'll initialize the server, set up routes, and handle WebSocket connections.

Image description

Image description

  1. Defining Models Define the Message and Conversation models in the model package.

Image description

Image description

  1. Connecting Database

Image description

  1. Implementing WebSocket Functionality Create the websocket package to manage WebSocket connections and messages.

WebSocketServer Structure
This structure holds information about the WebSocket server, including connected clients, channels for broadcasting messages, and services for conversations and messages.

Image description

Upgrader
The upgrader is used to upgrade HTTP connections to WebSocket connections. The CheckOrigin function allows connections from any origin.

Image description

NewWebSocketServer
This function creates a new instance of WebSocketServer, initializing its properties and returning it.

Image description

Run Method
This method starts the WebSocket server by listening on port 8081 and handling connections at the /ws/chat endpoint. It also starts a goroutine to handle messages.

Image description

HandleConnections Method
This method upgrades an HTTP connection to a WebSocket connection, registers the new connection, and listens for incoming messages.

Image description

handleMessages Method
This method listens for register, unregister, and broadcast events. It adds or removes clients from the list and sends broadcast messages to all connected clients.

Image description

Handling Actions
The server handles different types of actions received from clients, such as creating a conversation, fetching a conversation, and sending a message.

Create Conversation
This action creates a new conversation with the given sender and receiver IDs.

Image description

Get Conversation by ID
This action fetches a conversation by its ID and sends it back to the client.

Image description

Send Message
This action creates a new message in a conversation and sends the created message back to the client.

Image description

Conclusion
In this tutorial, we have built a simple chat application using Go, Gin, MongoDB, and WebSocket. We've covered setting up the server, defining models, and handling WebSocket connections. You can extend this application by adding more features like authentication, file uploads, and real-time notifications.

full code can be found
https://github.com/gbubemi22/goSetup.git

💖 💪 🙅 🚩
gbubemi22
oritsegbubemi

Posted on July 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related