DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 169 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 169 of my full-stack engineering track! Today, I built the core messaging dispatch engine (sendMessage and markMessagesAsSeen) for my real-time chat application, QuickChat! 💬⚡

This module ties together database persistence, media hosting, and WebSocket streaming into a single controller execution flow.


🛠️ Technical Breakdown: Messaging Controller Architecture

As captured in my implementation snapshots (messageController.js):

1. Cloudinary Integration & Mongoose Persistence

  • Checked incoming payloads for image attachments to upload directly via Cloudinary SDK before persisting document records into MongoDB:

javascript
  if (image) {
    const uploadResponse = await cloudinary.uploader.upload(image);
    imageURL = uploadResponse.secure_url;
  }
  const newMessage = await message.create({
    senderId,
    receiverId,
    text,
    image: imageURL
  }); const receiverSocketId = getReceiverSocketId(receiverId);
if (receiverSocketId) {
  getIO().to(receiverSocketId).emit('newMessage', newMessage);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)