npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

pocketbase-htmx-ext-sse

v0.0.3

Published

A pocketbase extension for htmx that adds SSE support

Readme

HTMX SSE for PocketBase

A custom Server-Sent Events (SSE) extension for HTMX that integrates with PocketBase's realtime API.

Overview

This extension builds upon the official HTMX SSE extension but is specifically designed to work with PocketBase's realtime API. It automatically handles PocketBase's connection protocol and subscription management.

Features

  • Automatic connection handling with PocketBase's realtime API
  • Seamless integration with HTMX's event system
  • Automatic subscription to specified topics
  • Support for dynamic content updates
  • Compatible with existing HTMX SSE event names and triggers

Installation

npm install pocketbase-htmx-ext-sse

or

<script src="https://unpkg.com/pocketbase-htmx-ext-sse"></script>

Usage

Add the extension to your HTML and specify the topics to subscribe to:

<div hx-ext="pocketbase-sse">
  <div sse-swap="chat" hx-swap="beforeend">
    <!-- Content from SSE events will be appended here -->
  </div>
</div>

<form hx-post="/api/chat" hx-swap="none">
  <input type="text" name="message" />
  <button type="submit">Send</button>
</form>

Backend Implementation

Using PocketPages

See PocketPages send() for more information.

Create an endpoint at /api/chat.ejs:

<script server>
  const { message } = body()
  send('chat', `<div>${message}</div>`)
  return { result: 'ok' }
</script>

Or using PocketPage's render capture feature for more complex content:

<script server>
  const { message } = body()
  send('chat')
</script>
<div class="chat-message">
  <div class="message-sender"><%= auth.email() %></div>
  <div class="message-content"><%= message %></div>
  <div class="message-timestamp"><%= new Date().toLocaleTimeString() %></div>
</div>

Using PocketBase JSVM

Create a custom API endpoint in your PocketBase app:

routerAdd('POST', '/api/chat', (c) => {
  const data = $apis.requestInfo(c).data
  const message = data.message

  // Get all clients from the subscription broker
  const clients = $app.subscriptionsBroker().clients()

  // Filter clients that are subscribed to the 'chat' topic
  Object.entries(clients)
    .filter(([_, client]) => client.hasSubscription('chat'))
    .forEach(([_, client]) => {
      // Send the message to each subscribed client
      client.send({
        name: 'chat',
        data: `<div>${message}</div>`,
      })
    })

  return c.json({ result: 'ok' })
})

This implementation:

  1. Gets all connected clients from PocketBase's subscription broker
  2. Filters for clients that are subscribed to the 'chat' topic
  3. Sends the formatted message directly to each subscribed client
  4. Returns a success response

SSE Message Format

When you connect to the realtime endpoint, you'll receive messages in this format:

id:NJMhEaQbJ4lJ7L5QIsenCCG8wzdH8mETTp2ncclX
event:PB_CONNECT
data:{"clientId":"NJMhEaQbJ4lJ7L5QIsenCCG8wzdH8mETTp2ncclX"}

id:NJMhEaQbJ4lJ7L5QIsenCCG8wzdH8mETTp2ncclX
event:chat
data:<div>asdf</div>

id:NJMhEaQbJ4lJ7L5QIsenCCG8wzdH8mETTp2ncclX
event:chat
data:"<div>\n  <p>This is a multiline message</p>\n  <p>That was JSON encoded</p>\n</div>"

The extension handles:

  1. The initial PB_CONNECT event to establish the connection
  2. Automatic subscription to your specified topics
  3. Swapping the data content into your elements based on the matching event name

Note: The extension will attempt to parse any JSON-parsable strings in the data field. This is particularly useful for multiline content, which should be JSON encoded to properly fit within a single line in the SSE data stream. If parsing fails (i.e., the content is not valid JSON), the original string is used.

Configuration Attributes

  • hx-ext="pocketbase-sse" - Activates the SSE extension
  • sse-swap="topic" - Defines which topic(s) to subscribe to (comma-separated for multiple topics)
  • hx-swap="beforeend" - (Optional) Controls how new content is inserted

How It Works

  1. When the extension initializes, it establishes a connection to PocketBase's realtime API
  2. Upon successful connection (PB_CONNECT event), it automatically subscribes to the specified topics
  3. Incoming messages on the subscribed topics trigger content updates according to the configured swap behavior

Events

This extension uses the same event names as the official HTMX SSE extension for compatibility:

  • htmx:sseOpen - Triggered when the SSE connection is established
  • htmx:sseMessage - Triggered when a message is received
  • htmx:sseError - Triggered when an error occurs
  • htmx:sseClose - Triggered when the connection is closed

You can use these events in your triggers and event handlers just like with the standard SSE extension.

Example with Multiple Topics

<div hx-ext="pocketbase-sse">
  <!-- Chat messages -->
  <div sse-swap="chat" hx-swap="beforeend">
    <!-- Chat messages will append here -->
  </div>

  <!-- User status updates -->
  <div sse-swap="users" hx-swap="innerHTML">
    <!-- User status will replace content here -->
  </div>
</div>

Advanced Usage

Custom Event Handling

You can listen for specific SSE events:

<div hx-ext="pocketbase-sse" sse-swap="chat" hx-trigger="sse:message">
  <!-- Handle specific message events -->
</div>

Error Handling

The extension automatically handles connection errors and reconnection attempts. You can style disconnected states using the sse-disconnected class that's added to the parent element when connection is lost.

.sse-disconnected {
  opacity: 0.5;
}

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.