Back to blog

Building Real-Time Chat with React and SignalR

October 27, 20245 min read

Learn how to build a dynamic chat application using React, SignalR, and Markdown streaming. This guide covers setting up the environment, integrating real-time messaging, and rendering Markdown content.

AI & Machine Learning Series — 25 articles
  1. Using ChatGPT for C# Development
  2. Trivia Spark: Building a Trivia App with ChatGPT
  3. Creating a Key Press Counter with Chat GPT
  4. Using Large Language Models to Generate Structured Data
  5. Prompt Spark: Revolutionizing LLM System Prompt Management
  6. Integrating Chat Completion into Prompt Spark
  7. WebSpark: Transforming Web Project Mechanics
  8. Accelerate Azure DevOps Wiki Writing
  9. The Brain Behind JShow Trivia Demo
  10. Building My First React Site Using Vite
  11. Adding Weather Component: A TypeScript Learning Journey
  12. Interactive Chat in PromptSpark With SignalR
  13. Building Real-Time Chat with React and SignalR
  14. Workflow-Driven Chat Applications Powered by Adaptive Cards
  15. Creating a Law & Order Episode Generator
  16. The Transformative Power of MCP
  17. The Impact of Input Case on LLM Categorization
  18. The New Era of Individual Agency: How AI Tools Empower Self-Starters
  19. AI Observability Is No Joke
  20. ChatGPT Meets Jeopardy: C# Solution for Trivia Aficionados
  21. Mastering LLM Prompt Engineering
  22. English: The New Programming Language of Choice
  23. Mountains of Misunderstanding: The AI Confidence Trap
  24. Measuring AI's Contribution to Code
  25. Building MuseumSpark - Why Context Matters More Than the Latest LLM

Building Real-Time Chat with React, SignalR, and Markdown Streaming

Building Real-Time Chat with React and SignalR

Introduction

Real-time chat looks simple from the outside — messages appear, characters stream in, the page stays smooth. The interesting work sits underneath: a React front end coordinating with SignalR for live messaging and a Markdown streaming pipeline that has to render incomplete content without flicker. Worth examining how the pieces fit together.

Prerequisites

Before we begin, ensure you have the following tools and knowledge:

  • Node.js and npm: Make sure you have Node.js and npm installed on your machine.
  • Basic knowledge of React: Familiarity with React components and hooks.
  • Understanding of TypeScript: Basic understanding of TypeScript is recommended.
  • SignalR basics: Some experience with SignalR will be helpful.

Setting Up the React Application

  1. Create a new React app:

    npx create-react-app real-time-chat --template typescript
  2. Install necessary packages:

    npm install @microsoft/signalr marked

Integrating SignalR for Real-Time Messaging

SignalR is a library that simplifies adding real-time web functionality to applications. It allows server-side code to push content to clients instantly.

Setting Up SignalR Client

  1. Create a SignalR connection:

    import * as signalR from "@microsoft/signalr";
    
    const connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").configureLogging(signalR.LogLevel.Information).build();
  2. Start the connection:

    connection.start().catch((err) => console.error(err.toString()));
  3. Handle incoming messages:

    connection.on("ReceiveMessage", (user, message) => {
        const msg = document.createElement("div");
        msg.textContent = `${user}: ${message}`;
        document.getElementById("messagesList").appendChild(msg);
    });

Implementing Markdown Streaming

Markdown allows users to format text using simple syntax. We will use the marked library to parse and render Markdown.

  1. Render Markdown messages:

    import { marked } from "marked";
    
    function renderMarkdown(message: string): string {
        return marked(message);
    }
  2. Display Markdown in the chat:

    const markdownMessage = renderMarkdown(message);
    document.getElementById("messagesList").innerHTML += `<div>${markdownMessage}</div>`;

Conclusion

By following these steps, you have successfully created a real-time chat application using React, SignalR, and Markdown streaming. This application allows users to send and receive messages in real-time while rendering Markdown content dynamically.

Additional Resources

Next Steps

Consider adding more features such as user authentication, message history, and emoji support to enhance your chat application.