pubsub

package module
v0.0.0-...-6e0bf2e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package pubsub provides simple pub/sub functionality for people who register on a given topic, functions will be called back in a thread-safe manner

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Publish

func Publish(key string, x interface{})

Publish a message with a payload.

Example

ExamplePubSub ... simple pub sub examples

package main

import (
	"log"
	"time"

	"github.com/DanielRenne/GoCore/core"
	"github.com/DanielRenne/GoCore/core/pubsub"
)

// TestPublish is a test struct for pubsub
type TestPublish struct {
	DeviceID string `json:"deviceId"`
	Value    bool   `json:"value"`
}

// ExamplePubSub ... simple pub sub examples
func main() {
	// Place me anywhere in your code
	pubsub.Subscribe("test", func(topic string, data interface{}) {
		// Do something with the data
		core.Debug.Dump("Dumping the data emojis wont work on windows because windows cmd line sucks, but on linux or mac it will kiss you 💋", topic, data)
		core.Debug.Dump("oh cool byte array dumps when non printables exist", "\x00 hello nulls!")
	})
	log.Print("Sleeping for 5 seconds")
	time.Sleep(time.Second * 5)
	core.Debug.Dump("Calling Publish")
	pubsub.Publish("test", TestPublish{DeviceID: "123", Value: true})
	log.Print("Sleeping for 2 seconds to allow the pubsub to run before it exits the program")
	time.Sleep(time.Second * 2)
}
Output:

	2022/10/03 21:25:30 Sleeping for 5 seconds
	!!!!!!!!!!!!! DEBUG 2022-10-03 21:25:35.506013!!!!!!!!!!!!!

	#### string                                  [len:15]####
	Calling Publish

	!!!!!!!!!!!!! ENDDEBUG 2022-10-03 21:25:35.506013!!!!!!!!!!!!!
	2022/10/03 21:25:35 Sleeping for 2 seconds to allow the pubsub to run before it exits the program

func Subscribe

func Subscribe(key string, callback SubscriptionCallback)

Subscribe to a publisher message key and function to call

Types

type SubscriptionCallback

type SubscriptionCallback func(key string, x interface{})

SubscriptionCallback is the callback function for published messgages for use in your interfaces

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL