🔒 You must be logged in as an Administrator or Editor to listen to this audio.
WeChat Mini-Program Chatbot with Orchard Core (LLM Agent) + Deep Dive: SSE as the Recommended Approach
This guide covers two core requirements:
- A complete end-to-end architecture for a WeChat Mini-Program chatbot that communicates with an LLM via Orchard Core (as the secure middle agent)
- A technical deep dive into why Server-Sent Events (SSE) is the only recommended real-time solution for this stack (WeChat Mini-Program + Orchard Core + LLM Chatbot)
We’ll tie every SSE benefit directly to WeChat Mini-Program’s unique constraints (the critical factor most guides ignore) and how it integrates perfectly with Orchard Core’s role as an LLM proxy.
1. System Architecture Overview
Core Stack
| Layer | Technology | Role |
|---|---|---|
| Frontend | WeChat Mini-Program (WXML/WXSS/TS/JS) | User chat interface; sends user messages; receives LLM streaming responses |
| Middleware Agent | Orchard Core (ASP.NET Core CMS) | Secure proxy for LLM; auth; chat history; content moderation; SSE streaming server; hides LLM API keys |
| AI Backend | LLM Model (OpenAI/Azure OpenAI/Llama 2) | Generates chat responses via streaming token output |
End-to-End Chat Flow
- User sends a message in the WeChat Mini-Program
- Mini-Program initiates an SSE connection to Orchard Core (HTTPS only)
- Orchard Core:
- Authenticates the WeChat mini-program user (via
wx.loginopenid) - Forwards the user message to the LLM’s streaming API
- Receives incremental LLM tokens (real-time)
- Pipes tokens directly to the Mini-Program via SSE
- Authenticates the WeChat mini-program user (via
- Mini-Program renders LLM tokens as they arrive (smooth chat typing effect)
- Orchard Core manages chat history, rate limiting, and WeChat-required content moderation
2. Deep Dive: Why SSE is NON-NEGOTIABLE for WeChat Mini-Programs
This is the critical analysis: SSE is not just a "good choice"—it is the only compliant, performant, and LLM-optimized solution for WeChat Mini-Program chatbots, due to WeChat’s strict platform constraints and the nature of LLM streaming chat.
Below is the technical breakdown of why SSE outperforms all alternatives (WebSocket, Polling, Long Polling) for this exact stack:
1. Perfect Alignment with LLM Streaming Chat (Unidirectional, Token-by-Token Push)
LLMs generate responses as incremental text tokens (not a single full response). A chatbot requires real-time rendering of these tokens (the "typing effect" users expect).
- SSE: Unidirectional server-to-client streaming protocol—exactly what chat needs (user sends one message; server streams the reply).
- LLM Fit: Orchard Core pipes raw LLM tokens directly to the Mini-Program via SSE (zero buffering, low latency).
- Alternatives:
- WebSocket: Bidirectional (overkill—wastes resources on unused client→server duplex)
- Polling/Long Polling: No real-time token streaming (user waits for full LLM response)
2. WeChat Mini-Program’s Strict WebSocket Limitations (Critical Platform Constraint)
WeChat imposes non-negotiable restrictions on WebSockets that make them unfeasible for chatbots:
- ✅ SSE: Uses standard HTTPS (no special permissions)
- ❌ WebSocket:
- Only 1 concurrent WebSocket connection allowed per Mini-Program (blocks other real-time features)
- Requires a separate WeChat domain whitelist (extra audit work)
- Higher handshake/resource overhead for mobile devices
- Orchard Core natively supports HTTPS—SSE integrates seamlessly with zero extra infrastructure.
3. Lightweight Client-Side Resource Usage (WeChat’s Sandboxed JS Runtime)
WeChat Mini-Programs run in a resource-constrained, sandboxed JavaScript engine (not a full browser). Performance/battery life is critical for user experience:
- SSE: Uses the native
EventSourceAPI—ultra-lightweight (minimal CPU/memory, no background timers). - WebSocket: Heavy duplex protocol handling (increased memory usage).
- Polling: Requires repeated timer loops (drains mobile battery, wastes bandwidth).
4. Native Compatibility with WeChat’s Network Stack
WeChat Mini-Programs ban raw TCP/UDP and restrict third-party real-time libraries—only native browser/WeChat APIs are allowed:
- ✅ SSE: WeChat natively supports the
EventSourceAPI (no plugins, no external libraries, 100% compliant). - ✅ Orchard Core: ASP.NET Core has built-in SSE support (no custom middleware—just set
Content-Type: text/event-stream). - ❌ Alternatives (MQTT, Socket.IO): Require plugins that fail WeChat’s audit.
5. Native Auto-Reconnection (Mobile Network Resilience)
Mobile users face frequent network drops (5G/Wi-Fi switching, weak signals):
- SSE: Built-in automatic reconnection with exponential backoff (no custom code needed). Orchard Core can resume the LLM stream seamlessly.
- WebSocket/Long Polling: Requires custom reconnection logic (prone to bugs in WeChat’s runtime).
- Polling: Fails silently during network drops.
6. Simplified Orchard Core Agent Implementation (Stateless, Low Complexity)
Orchard Core’s role is a stateless LLM proxy—SSE eliminates complex state management:
- SSE: Runs over standard HTTP (stateless); Orchard Core does not need to manage persistent connections/sessions.
- LLM Proxy: Orchard Core directly pipes the LLM’s HTTP streaming response to the SSE stream (1:1, zero code complexity).
- WebSocket: Requires Orchard Core to manage WebSocket sessions, connection state, and message routing (unnecessary overhead).
7. 100% WeChat Security & Audit Compliance
WeChat strictly audits Mini-Programs for data security and third-party API access:
- WeChat bans direct client-side calls to LLM APIs (exposes API keys, IP leaks, non-compliant).
- SSE + Orchard Core Proxy:
- All traffic uses HTTPS (WeChat mandate)
- LLM API keys are stored securely in Orchard Core (never exposed to the Mini-Program)
- Passes WeChat’s security audit with zero issues
- WebSockets have stricter audit scrutiny; direct LLM calls will get your Mini-Program rejected.
8. Bandwidth Efficiency (Mobile-First)
- SSE: Transmits only small LLM text tokens (minimal mobile data usage).
- Orchard Core: Supports gzip compression for SSE streams (further reduces bandwidth).
- WebSocket: Adds unnecessary frame overhead; Polling wastes data on empty requests.
3. Core Implementation (Code Snippets)
Below is the minimal production-ready code for the full stack.
Prerequisites
- WeChat Mini-Program: Enable HTTPS domain whitelisting for your Orchard Core server
- Orchard Core: .NET 7+/8, LLM SDK (e.g.,
OpenAINuGet) - LLM: Streaming API enabled (required for SSE)
3.1 WeChat Mini-Program: SSE Chat Client
Native EventSource implementation (WeChat-supported) for streaming chat:
// pages/chat/chat.ts (WeChat Mini-Program TypeScript)
Page({
data: {
message: "",
chatList: [] // [{ role: "user/bot", content: "text" }]
},
// Send user message to Orchard Core via SSE
async sendMessage() {
const { message } = this.data;
if (!message.trim()) return;
// Add user message to UI
this.setData({
chatList: [...this.data.chatList, { role: "user", content: message }],
message: ""
});
// 1. Get WeChat user code (auth for Orchard Core)
const { code } = await wx.login();
// 2. Initialize SSE connection to Orchard Core
const eventSource = new EventSource(`https://your-orchard-server.com/api/chat/stream?userCode=${code}&msg=${encodeURIComponent(message)}`);
// 3. Receive LLM tokens via SSE
let botReply = "";
eventSource.onmessage = (event) => {
const token = event.data;
if (token === "[DONE]") {
eventSource.close();
return;
}
// Stream tokens to UI (typing effect)
botReply += token;
this.setData({
chatList: [...this.data.chatList.filter(i => i.role !== "bot"), { role: "bot", content: botReply }]
});
};
// Error handling
eventSource.onerror = (err) => {
console.error("SSE Error:", err);
eventSource.close();
};
}
});
3.2 Orchard Core: SSE Streaming Endpoint (LLM Agent)
ASP.NET Core (Orchard Core) SSE server that proxies to the LLM and streams tokens to the Mini-Program:
// Orchard Core Controller (LLM Chat Agent)
using Microsoft.AspNetCore.Mvc;
using OpenAI_API;
using OpenAI_API.Chat;
[ApiController]
[Route("api/chat")]
public class ChatController : Controller
{
private readonly IOpenAIAPI _openAI;
public ChatController(IOpenAIAPI openAIAPI)
{
_openAI = openAIAPI; // Inject LLM client
}
// SSE Endpoint for WeChat Mini-Program
[HttpGet("stream")]
public async Task StreamChat(string userCode, string msg)
{
// 1. WeChat Auth (validate userCode via WeChat API)
// 2. Enable SSE response headers (critical for WeChat)
Response.Headers.Add("Content-Type", "text/event-stream");
Response.Headers.Add("Cache-Control", "no-cache");
Response.Headers.Add("Connection", "keep-alive");
Response.Headers.Add("X-Accel-Buffering", "no"); // Disable buffering
// 3. Call LLM Streaming API
var chatRequest = new ChatCreateRequest { Model = "gpt-3.5-turbo", Stream = true };
chatRequest.Messages.Add(new ChatMessage(ChatMessageRole.User, msg));
await foreach (var token in _openAI.Chat.CreateChatCompletionStreamAsync(chatRequest))
{
if (!string.IsNullOrEmpty(token.ToString()))
{
// 4. Send LLM token to Mini-Program via SSE (format: data: [token]\n\n)
await Response.WriteAsync($"data: {token}\n\n");
await Response.Body.FlushAsync();
}
}
// 5. Send stream end signal
await Response.WriteAsync("data: [DONE]\n\n");
await Response.Body.FlushAsync();
}
}
3.3 Orchard Core: LLM Integration
Register the LLM client in Orchard Core’s startup:
// Program.cs (Orchard Core)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenAIAPI("your-llm-api-key"); // LLM SDK
builder.Services.AddOrchardCore().AddMvc();
var app = builder.Build();
app.MapControllers();
app.Run();
4. WeChat Mini-Program SSE Best Practices
- HTTPS Only: WeChat blocks unencrypted HTTP for SSE
- Domain Whitelisting: Add your Orchard Core server to the WeChat Mini-Program backend domain whitelist
- Timeout Handling: Set SSE timeouts to match WeChat’s 60s request limit
- Content Moderation: Add Orchard Core moderation (WeChat requires chatbot content filtering)
- Rate Limiting: Use Orchard Core to limit LLM requests per user
- Chat History: Persist chat logs in Orchard Core’s built-in data storage
5. Summary
Key Takeaways
- Orchard Core is the ideal secure agent: it proxies LLM requests, hides credentials, and complies with WeChat’s security rules.
- SSE is the only recommended solution for WeChat Mini-Program chatbots because it:
- Adheres to WeChat’s strict WebSocket/network constraints
- Optimizes LLM token streaming
- Is lightweight, compliant, and mobile-friendly
- Integrates natively with Orchard Core (ASP.NET Core)
- Alternatives (WebSocket, Polling) fail WeChat’s platform rules or deliver a poor user experience for LLM chat.
This architecture is production-ready, WeChat audit-compliant, and optimized for the unique constraints of WeChat Mini-Programs.