Publish marshals data as JSON and publishes it to the given subject.
(subject string, data any)
| 147 | |
| 148 | // Publish marshals data as JSON and publishes it to the given subject. |
| 149 | func (c *Client) Publish(subject string, data any) error { |
| 150 | payload, err := json.Marshal(data) |
| 151 | if err != nil { |
| 152 | return fmt.Errorf("marshalling message for %s: %w", subject, err) |
| 153 | } |
| 154 | c.mu.RLock() |
| 155 | defer c.mu.RUnlock() |
| 156 | return c.conn.Publish(subject, payload) |
| 157 | } |
| 158 | |
| 159 | // Subscribe creates a subscription on the given subject. All subscribers receive every message. |
| 160 | func (c *Client) Subscribe(subject string, handler func([]byte)) (Subscription, error) { |