Skip to content

Commit 7536502

Browse files
committed
Real-Time Messaging with Pub/Sub in Taubyte
1 parent ad080c3 commit 7536502

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
title: "Real-Time Messaging with Pub/Sub in Taubyte"
3+
author: Zaoui Amine
4+
featured: true
5+
draft: false
6+
tags:
7+
- tutorials
8+
- messaging
9+
- pubsub
10+
- websocket
11+
- real-time
12+
- cloud
13+
image:
14+
src: /blog/images/pubsub-messaging.png
15+
alt: Real-Time Messaging with Pub/Sub in Taubyte
16+
summary: Build real-time features with Taubyte's built-in pub/sub messaging. Create WebSocket-enabled channels for chat, notifications, live updates, and more—all from your serverless functions.
17+
date: 2026-01-14T15:00:00Z
18+
categories: [Hand-on Learning]
19+
---
20+
21+
Taubyte includes built-in **pub/sub messaging** that enables real-time communication in your applications. With WebSocket support, you can build chat systems, live notifications, collaborative tools, and more—all directly from your serverless functions.
22+
23+
## Creating a Messaging Channel
24+
25+
From the sidebar, navigate to **Messaging** and click the **+** button.
26+
27+
Configure your channel:
28+
29+
| Field | Description | Example |
30+
|-------|-------------|---------|
31+
| Name | Channel identifier | `chat_channel` |
32+
| Topic Matcher | Topic pattern | `chat/rooms` |
33+
| WebSocket | Enable WebSocket support | ✅ Toggle on |
34+
35+
Click **Validate** to save.
36+
37+
### Pushing Changes
38+
39+
Click the **green push button**, review your changes, add a commit message, and push to the repository.
40+
41+
Build and deploy:
42+
43+
```bash
44+
dream inject push-all
45+
```
46+
47+
## Connecting Pub/Sub to Functions
48+
49+
Now let's create a function that provides WebSocket URLs for clients to connect.
50+
51+
### Create the WebSocket URL Function
52+
53+
Navigate to **Functions** and click **+**:
54+
55+
1. Click **Template Select**
56+
2. Choose **Go** and **get WebSocket URL**
57+
3. Set the domain to your generated domain
58+
4. Set the path to `/api/ws`
59+
60+
Switch to the **Code** tab and replace with:
61+
62+
```go
63+
package lib
64+
65+
import (
66+
"crypto/sha256"
67+
"encoding/hex"
68+
69+
"github.com/taubyte/go-sdk/event"
70+
"github.com/taubyte/go-sdk/pubsub"
71+
)
72+
73+
//export getWebSocketURL
74+
func getWebSocketURL(e event.Event) uint32 {
75+
h, err := e.HTTP()
76+
if err != nil {
77+
return 1
78+
}
79+
80+
// Get room name from query parameters
81+
query := h.Query()
82+
room := query.Get("room")
83+
84+
if room == "" {
85+
h.Write([]byte(`{"error": "room parameter required"}`))
86+
return 1
87+
}
88+
89+
// Hash the room name to create a unique channel
90+
hash := sha256.Sum256([]byte(room))
91+
channelName := hex.EncodeToString(hash[:])
92+
93+
// Open or create the pub/sub channel
94+
channel, err := pubsub.Open("chat/rooms/" + channelName)
95+
if err != nil {
96+
h.Write([]byte(`{"error": "failed to open channel"}`))
97+
return 1
98+
}
99+
defer channel.Close()
100+
101+
// Get WebSocket URL for this channel
102+
wsURL, err := channel.WebSocketURL()
103+
if err != nil {
104+
h.Write([]byte(`{"error": "failed to get websocket url"}`))
105+
return 1
106+
}
107+
108+
h.Write([]byte(wsURL))
109+
return 0
110+
}
111+
```
112+
113+
This function:
114+
1. Reads the `room` query parameter
115+
2. Hashes it to create a unique channel name
116+
3. Opens (or creates) the pub/sub channel
117+
4. Returns a WebSocket URL for clients to connect
118+
119+
Validate and push, then trigger a build:
120+
121+
```bash
122+
dream inject push-all
123+
```
124+
125+
## Testing with wscat
126+
127+
### Install wscat
128+
129+
First, install the WebSocket testing tool:
130+
131+
```bash
132+
npm install -g wscat
133+
```
134+
135+
### Get a WebSocket URL
136+
137+
Request a WebSocket URL for a room:
138+
139+
```bash
140+
curl "http://your-domain.blackhole.localtau:14529/api/ws?room=tau"
141+
```
142+
143+
Response (example):
144+
```
145+
/ws/channel/a8f5f167f44f4964e6c998dee827110c...
146+
```
147+
148+
### Connect Two Clients
149+
150+
Open **two terminal windows** to simulate a chat:
151+
152+
**Terminal 1:**
153+
```bash
154+
wscat -c "ws://your-domain.blackhole.localtau:14529/ws/channel/a8f5f167f44f4964e6c998dee827110c..."
155+
```
156+
157+
**Terminal 2:**
158+
```bash
159+
wscat -c "ws://your-domain.blackhole.localtau:14529/ws/channel/a8f5f167f44f4964e6c998dee827110c..."
160+
```
161+
162+
Now type in one terminal—it appears instantly in the other!
163+
164+
Your **pub/sub WebSocket is alive**.
165+
166+
167+
## Use Cases
168+
169+
| Use Case | Implementation |
170+
|----------|----------------|
171+
| Chat rooms | Per-room channels with WebSocket |
172+
| Live notifications | Global channel, functions publish |
173+
| Real-time dashboards | Data channels, subscribe from frontend |
174+
| Collaborative editing | Document-specific channels |
175+
| Game state sync | Game room channels |
176+
177+
178+
## Conclusion
179+
180+
You've learned how to:
181+
182+
1. **Create messaging channels** with WebSocket support
183+
2. **Build functions** that return WebSocket URLs
184+
3. **Test with wscat** for real-time communication
185+
4. **Build browser clients** for chat applications
186+
187+
Pub/sub is the foundation for real-time features in your applications—from chat to live notifications to collaborative tools.
188+
189+
Next, learn about [Applications](/blog/posts/taubyte-applications) for organizing resources into logical units.
190+

0 commit comments

Comments
 (0)