Simple Notifier/Listener utility package to pass around messages in-memory

Simple Observer (go)

A small simple library to use for sending around messages in-memory. It uses a notifier/listener style messaging.

Installation

go get -u github.com/danielp93/observer

Usage

  • Create notifiers by implementing the Notifier interface (or spawn a provided DefaultNotifier)

  • Create listeners by implementing the Listener interface (or spawn a provided DefaultListener by calling ListenerFunc(ListenFunc))

  • A ListenerFunc is a function that takes a Message and calls that function whenever the Listener is Notified

  • Notify Listeners by subscribing them to Notifiers and calling the Notify(Message) method receiver on the Notifier

  • For advanced customization the library provides interfaces

  • Be aware that this

package main

import (
	"fmt"

	"github.com/danielp93/observer"
)

func main() {
	// Create Notifier
	n := observer.NewDefaultNotifier()
	if n == nil {
		return
	}
	n2 := observer.NewDefaultNotifier()
	if n == nil {
		return
	}

	// Create Listener from Listenerfunc (func(Message))
	// This is a simple Listener that will print out the message
	// If message is a Simplemessage it also will print the type and timestamp
	l := observer.ListenerFunc(func(message observer.Message) {
		switch m := message.(type) {
		case *observer.SimpleMessage:
			fmt.Printf("Received Message [%s] %s @%v\n", m.Type, m.Message, m.Timestamp)
		default:
			fmt.Println(message)
		}
	})
	// listener uses a channel under the hood, Close it for garbage collection
	defer l.Close()

	// Subscribe Listener to Notifiers
	n.Subscribe(l)
	n2.Subscribe(l)

	m := observer.NewMessage("Hello from the other side!", "ECHO")
	m2 := observer.NewMessage("Hello also from me!", "ECHO2")

	n.Notify(m)
	n2.Notify(m2)
}
$ go run observer/examples/echo.go

Received Message [ECHO] Hello from the other side! @2021-12-03 21:08:01.5650042 +0100 CET m=+0.000041501
Received Message [OTHERECHO] Hello also from me! @2021-12-03 21:08:01.5650054 +0100 CET m=+0.000042601
Similar Resources

Simple script for farm free books from PackPub.com

Bookgot BookGot is a simple boot for farm free books from PACKTPUB.COM #Install go get -u github.com/bregydoc/Bookgot Usage First import BookGot //..

Mar 26, 2019

`go-redash-query` is a simple library to get structed data from `redash query` sources

go-redash-query go-redash-query is a simple library to get structed data from redash query sources Example Source table id name email 1 Dannyhann rhrn

May 22, 2022

Go-serverless-eth-event-listener - Go serverless, ethereum contract event listener with a sample contract

go-serverless-eth-event-listener This repository is for showing how to listen sm

May 19, 2022

W5-test-go - Write functions to pass the tests with the cases need to pass

Week 5 Assignment In this assignment, we expect to you write functions to pass t

Feb 11, 2022

🌍 Package tcplisten provides a customizable TCP net.Listener with various performance-related options

Package tcplisten provides customizable TCP net.Listener with various performance-related options: SO_REUSEPORT. This option allows linear scaling ser

Nov 14, 2022

Simple Apple Software Release Notifier, sends an email containing release information

Apple Release Notifier Simple Apple Software Release Notifier, sends an email containing release information. Usage Fill in the contacts file with the

Feb 22, 2022

Lms-notifier - A simple tool to keep track of lms modules

lms-notifier A simple tool to keep track of lms modules Getting Started These in

Sep 16, 2022

(WIP) Extremely simple unixway GitHub webhook listener for push event

(WIP) puffy Puffy is an extremely simple unixway GitHub webhook listener and handler for push events Todo Add payload signature validation (WIP) Depen

Oct 15, 2022

gosx-notifier is a Go framework for sending desktop notifications to OSX 10.8 or higher

gosx-notifier is a Go framework for sending desktop notifications to OSX 10.8 or higher

gosx-notifier A Go lib for sending desktop notifications to OSX Mountain Lion's (10.8 or higher REQUIRED) Notification Center. Update 4/3/2014 On OSX

Dec 28, 2022

CoWIN Vaccine availability notifier for India

CoWIN Vaccine availability notifier for India

covaccine-notifier CoWIN Portal Vaccine availability notifier covaccine-notifier periodically checks and sends email notifications for available slots

Sep 6, 2022

Notifie Server is a learning list notifier

Notifie Server Notifie Server is a learning list notifier. E.g; If you want to l

Dec 24, 2021

Notifier is a Go library to send notification to other applications.

Notifier is a Go library to send notification to other applications.

Notifier is a Go library to send notification to other applications.

Oct 26, 2022

Golang MySql binary log replication listener

Go MySql binary log replication listener Pure Go Implementation of MySQL replication protocol. This allow you to receive event like insert, update, de

Oct 25, 2022

v2 of the veai listener. Now with channels!

veai-listener This is a pipeline tool to allow for watching a dir that takes in video that needs to be upscaled. The dir is accessed via Dropbox and i

Dec 8, 2021

The Webhooks Listener-Plugin library consists of two component libraries written in GoLang

The Webhooks Listener-Plugin library consists of two component libraries written in GoLang: WebHook Listener Libraries and Plugin (Event Consumer) Libraries.

Feb 3, 2022

CircleHash is a family of fast hashes that pass SMHasher, are faster than XXH64, SipHash, etc. and are easy to audit

CircleHash CircleHash is a family of non-cryptographic hash functions that pass every test in SMHasher (both rurban/smhasher and demerphq/smhasher). T

Sep 18, 2022

Go implementation Welford’s method for one-pass variance computation

Welford - Online method of calculating variance and standard deviation Go implementation Welford’s method for one-pass variance computation with D. H.

Jan 6, 2023

Go implementation Welford’s method for one-pass variance computation

Variance and standard deviation caluculation using variance's algorithm Table of Contents Introduction Installation Usage Contributing License Introdu

Jun 5, 2022

Implementation for validating the NZ COVID Pass.

NZCP validator Validates NZCP passes according to https://nzcp.covid19.health.nz. Example See example_test.go and tests for more examples. func Exampl

Dec 20, 2021
A go wrapper around the rwkv.cpp library

AI without python (1) go-rwkv.cpp gowrkv.go is a wrapper around rwkv-cpp, which is an adaption of ggml.cpp. (1) Python required for training and conve

May 10, 2023
Example skills and a cli utility written in Go for interacting with Webex Assistant Skills

Webex Assistant Skills - Go This repository holds example skills and a cli utility written in Go for interacting with Webex Assistant Skills. It is in

Oct 29, 2021
An experimental generic functional utility library inspired by Lodash

go-godash An experimental generic functional utility library inspired by Lodash Implemented functions Map Reduce Sum Filter Take TakeWhile Drop DropWh

May 31, 2022
Generic utility methods for Go slices / arrays / collections, heavily inspired by Lodash.

slicy import "github.com/sudhirj/slicy" Usage func All func All[S ~[]T, T any](slice S, predicate func(value T, index int, slice S) bool) bool All re

Aug 30, 2022
My ML - A machine learning package based on golang

my_ML a machine learning package based on golang 这是一个基于golang的机器学习库和一些机器学习的数据集,可

Feb 15, 2022
An open source programming language that makes it easy to build simple
An open source programming language that makes it easy to build simple

The Go Programming Language Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. Gopher ima

Oct 15, 2021
This is an example of a keep-it-simple directory layout for Go projects that was created using DDD principles, please copy and share if you like it.

DDD Go Template This project was created to illustrate a great architectural structure I developed together with @fabiorodrigues in the period I was w

Dec 5, 2022
Repository for COMP 429 Programming Assignment# 1 - A simple chat application, by Sabra Bilodeau.

Chatty COMP 429 Programming Assignment 1 A Chat Application for Remote Message Exchange by Sabra Bilodeau Getting Started with Chatty Building the Pro

Nov 28, 2021
Simple boilerplate code to get started with building and deploying a serverless CRUD API

Simple boilerplate code to get started with building and deploying a serverless CRUD API with Go, MongoDB and Netlify

Jan 20, 2022
Simple first method GRPC on GO

Simple first method GRPC on GO

Dec 10, 2021