# PubSub

This package provides a simple pub/sub implementation using the NATS messaging system. It allows users to subscribe and publish events and messages easily.

## Installation

To use this package in your project, add the following import statement:

```
import "github.com/pingponglabs/mediamagic-platform/libs/pubsub"
```

## Usage

### Creating a new PubSub client

```go
import (
	"github.com/nats-io/nats.go"
	"go.uber.org/zap"
)

// ...

conn, _ := nats.Connect(nats.DefaultURL)
logger, _ := zap.NewProduction()
pubsubClient := pubsub.New(conn, logger)
```

### Subscribing to a generic event

```go
callback := func(value []byte) {
	// Process the message here
}

key := "message-key"
err := pubsubClient.Subscribe(key, callback)

if err != nil {
	// Handle error here
}
```

### Subscribing to a typed event

```go
callback := func(value *pubsub.Event) {
	// Process the message here
}

key := "message-key"
err := pubsubClient.SubscribeEvent(key, callback)

if err != nil {
	// Handle error here
}
```

### Publishing a generic event

```go
key := "message-key"
value := []byte("Hello, World!")
err := pubsubClient.Publish(key, value)

if err != nil {
	// Handle error here
}
```

### Publishing a typed event

```go
key := "event-key"
event := pubsub.Event{
	ResourcedID: id, // for example, deployment ID
	ResourceType: "deployment",
    Value: 1, // As in, 1 created
	OwnerID: ownerID, // The user who created the deployment
	Role: "user",
	EventType: "created",
	Metadata: map[string]string{
        "model": "image-classification",
    }, // Any additional metadata
}

err := pubsubClient.PublishEvent(key, event)
if err != nil {
	// Handle error here
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mediamagic.dev/product-docs/libs/pubsub.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
