SSE Server-Side Real-time Push
Overview
HttpServer has built-in SSE (Server-Sent Events) support, enabling server-to-client real-time message push. Suitable for:
- Real-time notifications
- Online chat
- Data monitoring
- Progress push
Client Request
Client connects using cSSEClient:
vb
Private WithEvents SSE As cSSEClient
Private Sub Connect()
Set SSE = New cSSEClient
SSE.Connect "http://localhost:8080/events"
End Sub
Private Sub SSE_OnMessage(EventName As String, Data As String, Id As String)
Debug.Print "Received: " & EventName & " = " & Data
End SubServer Implementation
Basic Push
vb
' Push message in controller
Public Sub Events(ctx As cHttpServerContext)
' Set SSE headers
ctx.Response.Header("Content-Type") = "text/event-stream"
ctx.Response.Header("Cache-Control") = "no-cache"
ctx.Response.Header("Connection") = "keep-alive"
' Get SSE instance
Dim sse As cSSE
Set sse = ctx.SSE
' Send message
Call sse.Send(ctx.ClientInfo.hSocket, "message", "Hello World", "1")
' Send JSON data
Dim data As New Scripting.Dictionary
data("time") = Now
data("status") = "ok"
Call sse.Send(ctx.ClientInfo.hSocket, "data", Json.Encode(data), "2")
End SubBroadcast Message
vb
' cNotificationController.cls
' Send to all connections
Public Sub Broadcast(ctx As cHttpServerContext)
Dim msg As String
msg = ctx.Request.Form("message")
' Broadcast to all SSE clients
Call ctx.SSE.Broadcast("notification", msg)
ctx.Response.Json Nothing, 0, "Broadcast sent"
End Sub
' Send to specific user
Public Sub SendToUser(ctx As cHttpServerContext)
Dim userId As String, msg As String
userId = ctx.Request.Form("user_id")
msg = ctx.Request.Form("message")
' Get user's socket
Dim hSocket As Long
hSocket = GetUserSocket(userId)
If hSocket > 0 Then
Call ctx.SFE.Send(hSocket, "private", msg)
ctx.Response.Json Nothing, 0, "Sent"
Else
ctx.Response.Json Nothing, 1, "User offline"
End If
End SubReal-time Data Stream
vb
' cMonitorController.cls
' Real-time system monitoring
Public Sub Monitor(ctx As cHttpServerContext)
' Set SSE headers
ctx.Response.Header("Content-Type") = "text/event-stream"
Dim sse As cSSE
Set sse = ctx.SSE
Dim counter As Long
counter = 0
' Continuous push (use timer in real applications)
Do While sse.IsConnected(ctx.ClientInfo.hSocket)
counter = counter + 1
' Collect system data
Dim data As New Scripting.Dictionary
data("cpu") = GetCPUUsage()
data("memory") = GetMemoryUsage()
data("time") = Now
' Send
Call sse.Send(ctx.ClientInfo.hSocket, "stats", Json.Encode(data), CStr(counter))
' Push once per second
Sleep 1000
Loop
End SubProgress Push
vb
' cTaskController.cls
' Execute task and push progress
Public Sub RunTask(ctx As cHttpServerContext)
Dim taskId As String
taskId = ctx.Request.Form("task_id")
' Set SSE
ctx.Response.Header("Content-Type") = "text/event-stream"
Dim sse As cSSE
Set sse = ctx.SSE
Dim i As Long
' Simulate long-running task
For i = 0 To 100 Step 10
' Execute task...
DoTaskWork i
' Push progress
Dim progress As New Scripting.Dictionary
progress("percent") = i
progress("task_id") = taskId
Call sse.Send(ctx.ClientInfo.hSocket, "progress", Json.Encode(progress))
Sleep 500
Next i
' Complete
Call sse.Send(ctx.ClientInfo.hSocket, "complete", "{\"status\":\"done\"}")
End SubComplete Chat Example
vb
' ========== Server ==========
' cChatController.cls
Option Explicit
' GET /chat/stream
Public Sub Stream(ctx As cHttpServerContext)
' Check login
If Not ctx.Session.Exists("user_id") Then
ctx.Response.State401 "Please login first"
Exit Sub
End If
' Set SSE headers
ctx.Response.Header("Content-Type") = "text/event-stream"
ctx.Response.Header("Cache-Control") = "no-cache"
ctx.Response.Header("Connection") = "keep-alive"
' Register to chat room
Dim userId As String, username As String
userId = ctx.Session("user_id")
username = ctx.Session("username")
Call RegisterUser(userId, username, ctx.ClientInfo.hSocket)
' Send welcome message
Call ctx.SSE.Send(ctx.ClientInfo.hSocket, "system", _
"{\"msg\":\"Welcome " & username & " to the chat room\"}")
' Keep connection
Do While ctx.SSE.IsConnected(ctx.ClientInfo.hSocket)
DoEvents
Sleep 100
Loop
' Remove user on disconnect
Call UnregisterUser(userId)
End Sub
' POST /chat/send
Public Sub Send(ctx As cHttpServerContext)
Dim msg As String
msg = ctx.Request.Form("message")
Dim username As String
username = ctx.Session("username")
' Broadcast to all users
Dim data As String
data = "{\"user\":\"" & username & "\",\"msg\":\"" & msg & "\"}"
Call ctx.SSE.Broadcast("message", data)
ctx.Response.Json Nothing, 0, "Sent"
End Sub
' ========== Client ==========
Private WithEvents SSE As cSSEClient
Private Sub JoinChat()
Set SSE = New cSSEClient
SSE.Connect "http://localhost:8080/chat/stream"
End Sub
Private Sub SendMessage(msg As String)
Dim http As New cHttpClient
http.RequestDataForm("message") = msg
http.SendPost "http://localhost:8080/chat/send"
End Sub
Private Sub SSE_OnMessage(EventName As String, Data As String, Id As String)
Select Case EventName
Case "system"
' Show system message
ShowSystemMessage Data
Case "message"
' Show chat message
ShowChatMessage Data
End Select
End SubClient Management
vb
' cSSE.cls (built-in framework)
' Get online client count
dim count as long
count = ctx.SSE.ClientCount
' Close specific client
call ctx.SSE.CloseClient(hSocket)
' Close all clients
call ctx.SSE.CloseAll()Last Updated: 2026-05-17