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.

Creating a Messaging Channel

From the sidebar, navigate to Messaging and click the + button.

Creating a pub/sub channel

Configure your channel:

FieldDescriptionExample
NameChannel identifierchat_channel
Topic MatcherTopic patternchat/rooms
WebSocketEnable WebSocket support✅ Toggle on

Click Validate to save.

Pushing Changes

Click the green push button, review your changes, add a commit message, and push to the repository.

Build and deploy:

dream inject push-all

Connecting Pub/Sub to Functions

Now let’s create a function that provides WebSocket URLs for clients to connect.

Create the WebSocket URL Function

Navigate to Functions and click +:

  1. Click Template Select
  2. Choose Go and get WebSocket URL

Creating get WebSocket URL function

  1. Set the domain to your generated domain
  2. Set the path to /api/ws

Switch to the Code tab and replace with:

package lib

import (
    "crypto/sha256"
    "encoding/hex"

    "github.com/taubyte/go-sdk/event"
    "github.com/taubyte/go-sdk/pubsub"
)

//export getWebSocketURL
func getWebSocketURL(e event.Event) uint32 {
    h, err := e.HTTP()
    if err != nil {
        return 1
    }

    // Get room name from query parameters
    query := h.Query()
    room := query.Get("room")

    if room == "" {
        h.Write([]byte(`{"error": "room parameter required"}`))
        return 1
    }

    // Hash the room name to create a unique channel
    hash := sha256.Sum256([]byte(room))
    channelName := hex.EncodeToString(hash[:])

    // Open or create the pub/sub channel
    channel, err := pubsub.Open("chat/rooms/" + channelName)
    if err != nil {
        h.Write([]byte(`{"error": "failed to open channel"}`))
        return 1
    }
    defer channel.Close()

    // Get WebSocket URL for this channel
    wsURL, err := channel.WebSocketURL()
    if err != nil {
        h.Write([]byte(`{"error": "failed to get websocket url"}`))
        return 1
    }

    h.Write([]byte(wsURL))
    return 0
}

This function:

  1. Reads the room query parameter
  2. Hashes it to create a unique channel name
  3. Opens (or creates) the pub/sub channel
  4. Returns a WebSocket URL for clients to connect

Validate and push, then trigger a build:

dream inject push-all

Testing with wscat

Install wscat

First, install the WebSocket testing tool:

npm install -g wscat

Get a WebSocket URL

Request a WebSocket URL for a room:

curl "http://your-domain.blackhole.localtau:14529/api/ws?room=tau"

Response (example):

/ws/channel/a8f5f167f44f4964e6c998dee827110c...

Connect Two Clients

Open two terminal windows to simulate a chat:

Terminal 1:

wscat -c "ws://your-domain.blackhole.localtau:14529/ws/channel/a8f5f167f44f4964e6c998dee827110c..."

Terminal 2:

wscat -c "ws://your-domain.blackhole.localtau:14529/ws/channel/a8f5f167f44f4964e6c998dee827110c..."

Now type in one terminal—it appears instantly in the other!

Two terminals with wscat for pub/sub testing

Your pub/sub WebSocket is alive.

Use Cases

Use CaseImplementation
Chat roomsPer-room channels with WebSocket
Live notificationsGlobal channel, functions publish
Real-time dashboardsData channels, subscribe from frontend
Collaborative editingDocument-specific channels
Game state syncGame room channels

Conclusion

You’ve learned how to:

  1. Create messaging channels with WebSocket support
  2. Build functions that return WebSocket URLs
  3. Test with wscat for real-time communication
  4. Build browser clients for chat applications

Pub/sub is the foundation for real-time features in your applications—from chat to live notifications to collaborative tools.

Next, learn about Applications for organizing resources into logical units.