
Watch front-end demo of a real-time chat backend built with Go, WebSockets, and JWT auth. See sign up, login, online presence, message delivery, read receipts, and image attachments in action.
Install Go on your system to follow along; Windows and Mac installations are straightforward. Linux users can consult ai model for setup and install Visual Studio Code with Go extension.
Access the course repository on GitHub to get the full backend and the mobile app demoed in the first video.
Explain graceful shutdown using a go routine and channel to stop new requests, finish ongoing work, and close the shutdown channel to prevent leaks.
Register a health check http endpoint in a go server using mux and handle func, returning 'API is running' at /api/health-check-http to verify readiness.
Extract endpoints from main.go into a routes package with registerRoutes and healths.go to handle health checks and login, signup, getuser, logout, getmessages, and getconversations.
Add two middlewares: a logging middleware that outputs the request method, URL, proto, and remote address, and a cross-origin middleware to enable front-end development.
Builds a signup endpoint and handler in Go, decoding JSON with name, email, and password, validating input, hashing the password, creating the user, and returning 201 created.
Define a Go user model with id, name, email, password, and refresh tokens; then implement getUserByEmail and createUserByEmail to query and insert users in the database for signup.
Implement email login by creating a /api/auth/login-email endpoint, validating email and password, and generating access and refresh tokens with platform-specific refresh token storage for web or mobile.
Log in to obtain a refresh token, send it to refresh the session, and receive a new access token and refresh token, then prepare to fetch user data.
Complete the authentication flow by refreshing the session and retrieving the current user using the access and refresh tokens, via the get current-user endpoint secured by authentication middleware.
Define the private model with id, user1, user2, and created_at, enforce unique user pairs by ordering ids, and implement Go db functions to get by id, by users, and create.
Build a Go message model with ID, from ID, private ID, message type, content, delivered, read, created at; implement SQLite database functions for create, fetch with pagination, and update delivered/read.
Implement conversation endpoints to manage private chats by checking existing conversations with endpoint /conversations/privates/{private_id}, creating or joining via post request, retrieving past conversations on restart, and fetching messages per private.
Implement the Go get private-by-id handler: authenticate endpoints, extract and parse private id from the path, fetch private via the model, and respond with data or errors.
Upload files via a post /files/{private_id} endpoint, authenticate and parse a multipart form up to 50 megabytes, save the file to /files/chats/{private_id}/{sender_id}/{filename}, and return the accessible file URL.
Install and configure a WebSocket endpoint to upgrade HTTP connections to WebSocket, then echo back client messages on a health check ws endpoint using wsjson for real-time communication.
Implement hub.broadcastToAll in Go to broadcast an event to all connections, using read locks, iterating clients and their connections, and sending via send channels, dropping if the buffer is full.
Define hub method sendEventToUserIds to broadcast an event from the sender to multiple users by iterating IDs, acquiring read lock, locating connections, and sending the event with type and payload.
Register a new client connection in the hub and detect first-time logins. Broadcast the user online to all clients and asynchronously deliver undelivered messages to the joining user.
Unregister a client by locking the hub, removing the client's connection, and broadcasting a user offline event to everyone when no connection left.
Implement the hub's sendError method to dispatch an error event with the given message. Retrieve the client's connections with getClient and send the error event to each connection.
Lock the hub, notify all clients with a shutdown event payload 'server is shutting down', close each connection, then clear the client map and log completion.
Extract bearer token from authorization header, verify the JWT to identify the user, upgrade to a WebSocket, register the client to the hub, and start heartbeat, writePump, and readPump.
Implement a goroutine heartbeat that sends a heartbeat event every 30 seconds over WebSocket to verify frontend connectivity, using context with timeout and error handling.
Split WebSocket handling into readPump and writePump, and implement writePump to listen for ctx.done and client.send. On send, create a timeout context, write with ws.json to the client, then cancel.
Implement a readPump in Go to read websocket messages from the front end with context cancellation. Split logic to handleIncomingEvent with panic recovery and event dispatch to hub and client.
Implement handleIncomingEvent to process real-time events using the hub and client, validate the payload as a string-keyed map, and handle message, delivered, read, and typing events.
Handle real-time event delivered in a websocket chat by validating the message id, fetching the message, preventing self-delivery, and marking as delivered and read with a delivered event.
Implement real-time typing indicators by extracting private and receiver IDs, converting isTyping to boolean, and sending a typing event to the receiver with private id, user id, and isTyping.
Correct the getCurrentUser handler to use a post request, since it expects a body with the refresh token. Update the main to switch to post.
Correct the toMap function in user.go to map user fields with the correct keys, name and email, ensuring accurate model data for real-time chat with Go websockets and JWT auth.
Correct the hub.go send current clients logic by continuing only when the status is ok, ensuring proper error handling in the WebSocket hub.
Unlock the power of Go (Golang) by building a fully functional real-time chat application from scratch! In this course, you’ll learn how to develop a production-ready backend using Go, implement secure JWT authentication, manage WebSocket connections for real-time messaging, and handle file uploads and downloads. This course is designed to guide you step by step, from setting up your project structure to deploying a scalable and robust chat system.
We start with the basics, including module and folder organization, and gradually move to more advanced topics such as database design, handling private conversations, message pagination, and user management. You’ll also implement middleware for logging, CORS, and authentication to ensure your app follows best practices.
A major focus of this course is real-time communication. You’ll learn how to manage WebSocket clients, send events for messages, typing indicators, delivery and read receipts, and maintain active connections using heartbeats. We’ll also cover graceful shutdowns, error handling, and proper server initialization.
By the end of this course, you will have a professional-grade chat backend that you can deploy anywhere. Whether you’re a beginner wanting to learn Go through a practical project, or an experienced developer looking to expand your backend and real-time programming skills, this course provides all the tools, guidance, and knowledge you need.
Start building your Go chat app today and take your backend development skills to the next level!