DEV Community

Cover image for Building a Real-Time Notification Panel with Tailwind CSS and WebSockets
HexShift
HexShift

Posted on

Building a Real-Time Notification Panel with Tailwind CSS and WebSockets

Adding real-time features to your web application can significantly enhance user experience by keeping interfaces fresh and interactive without requiring full page reloads. One common example is a live notification panel that receives updates via WebSockets. In this tutorial, we’ll explore how to build a clean, responsive notification UI using Tailwind CSS for styling and WebSockets for real-time updates — all in a framework-agnostic way.


Building the Notification Panel UI

The interface includes a notification icon that toggles a dropdown panel displaying the latest messages.

  • A relative wrapper positions the icon and panel together.
  • The dropdown panel is styled with:
<div class="absolute right-0 mt-2 w-80 bg-white dark:bg-gray-800 shadow-lg rounded-lg overflow-hidden z-50">
  <!-- Notifications go here -->
</div>
Enter fullscreen mode Exit fullscreen mode

This ensures your dropdown floats above the UI while supporting dark mode.


Notification Item Styling

Each notification is styled like a card using Tailwind's flex and spacing utilities:

<div class="flex items-start px-4 py-3 border-b border-gray-200 dark:border-gray-700">
  <div class="flex-1">
    <p class="font-semibold text-gray-800 dark:text-gray-100">New Comment</p>
    <p class="text-sm text-gray-600 dark:text-gray-300">Alice replied to your post.</p>
    <p class="text-xs text-gray-400">Just now</p>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

This keeps each message compact, legible, and visually clean.


Real-Time WebSocket Integration

The WebSocket backend can be any platform — Django Channels, Phoenix PubSub, Node.js with Socket.IO, etc.

On the frontend:

  • Use a native WebSocket or a framework-based abstraction.
  • In plain JavaScript:
const socket = new WebSocket("wss://example.com/notifications");
socket.onmessage = (event) => {
  const data = JSON.parse(event.data);
  appendNotification(data);
};
Enter fullscreen mode Exit fullscreen mode
  • In frameworks like React or Alpine.js, update a reactive array to automatically rerender new notifications.

Notification Icon with Badge

Add a red dot to indicate new notifications with this utility combo:

<span class="absolute -top-1 -right-1 bg-red-600 rounded-full w-4 h-4 text-xs text-white flex items-center justify-center">
  3
</span>
Enter fullscreen mode Exit fullscreen mode

Position it on top of a bell icon to give users a subtle but clear visual cue.


Making It Responsive

Tailwind makes responsiveness easy:

  • Use w-full sm:w-80 to adapt the dropdown size.
  • Apply max-h-96 overflow-y-auto to allow scrolling for longer lists.
  • Enhance transitions with transition-opacity duration-200 for smooth fades.

These utilities ensure the panel works beautifully on any screen size.


Performance Considerations

  • Tailwind’s JIT mode compiles only used styles, keeping CSS lean.
  • WebSockets offer low-latency, low-bandwidth communication.
  • Together, this pattern creates a lightweight, elegant real-time experience.

Scale with Confidence

If this is just one component in a growing system, maintaining consistency and performance can get tricky.

That’s why I wrote a 37-page guide:

Mastering Tailwind at Scale: Architecture, Patterns & Performance

Inside, you'll find:

  • Utility class architecture strategies
  • Scalable component patterns
  • Performance tuning for production
  • Tips for working with Tailwind across teams and frameworks

Upgrade your real-time interfaces with Tailwind and WebSockets — and take your UI design to the next level.

Top comments (0)