efmq

package module
v0.0.0-...-f36ddc2 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2017 License: MIT Imports: 6 Imported by: 0

README

Ethernet Frames Message Queue (EFMQ)

EFMQ provides an MQTT pub/sub style abstraction for Ethernet Frame messaging.

GoDoc

EFMQ is like MQTT for your Local Area Network. Unlike MQTT no remote or local broker is required, message traffic is effectively broadcast peer-to-peer. With EFMQ, messages never leave your network.

Messaging can be two-way. Each node can operate as either a publisher or subscriber, or both.

This package leans heavily on @mdlayher's raw and ethernet packages, which do almost all the heavy lifting.

Usage

Basic publisher and subscriber examples are provided below. Nodes can publish and subscribe to multiple topics.

The API follows a typical MQTT client API loosely.

// Create connection
mq, _ := efmq.NewEFMQ(networkInterface string)

// Publish
mq.Publish(topic string, payload string)

// Subscribe
mq.Subscribe(topic string)

// Unsubscribe
mq.Unsubscribe(topic string)

// List subscriptions
mq.Subscriptions()

// Start listening
mq.Listen()

// Message channel
mq.Message

// Message 
Message struct {
	Topic string
	Payload string
}
Publisher example

The code below will publish data to the fermenter topic every second. en1 is the network interface on Mac (my Mac at least). On a Raspberry Pi it might be wlan0. Use netstat -i to discover.

mq, err := efmq.NewEFMQ("en1") 
if err != nil {
	log.Fatal(err)
}
t := time.NewTicker(1 * time.Second)
for range t.C {
	if err := mq.Publish("fermenter", "20.5"); err != nil {
		log.Fatalln(err)
	}
}
Subscriber example

The code below sets up a subcription to the fermenter topic and then listens for messages. Messages are received on a channel.

mq, err := efmq.NewEFMQ("wlan0")
if err != nil {
	log.Fatal(err)
}
mq.Subscribe("fermenter")
mq.Listen()
for msg := range mq.Message {
	fmt.Println("topic:", msg.Topic)
	fmt.Println("message:", msg.Payload)
}

Todo

  • Better test coverage
  • Check message does not exceed frame byte data limit (1500 bytes?)

Documentation

Overview

Package efmq provides basic MQTT like functionality for message publishing and subscriptions within a local area network

Example (Publisher)

This example starts publishing to a topic every second via the network interface "wlan0".

package main

import (
	"log"
	"time"

	"github.com/olliephillips/efmq"
)

func main() {
	mq, err := efmq.NewEFMQ("wlan0")
	if err != nil {
		log.Fatal(err)
	}
	t := time.NewTicker(1 * time.Second)
	for range t.C {
		if err := mq.Publish("fermenter", "20.5"); err != nil {
			log.Fatalln(err)
		}
	}
}
Output:

Example (Subscriber)

This example sets up a subscription to a topic and starts listening for messages from a device on the same network.

package main

import (
	"fmt"
	"log"

	"github.com/olliephillips/efmq"
)

func main() {
	mq, err := efmq.NewEFMQ("wlan0")
	if err != nil {
		log.Fatal(err)
	}
	mq.Subscribe("fermenter")
	mq.Listen()
	for msg := range mq.Message {
		fmt.Println("topic:", msg.Topic)
		fmt.Println("message:", msg.Payload)
	}
}
Output:

fermenter
20.5
fermenter
20.5
fermenter
20.5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EFMQ

type EFMQ struct {
	Message chan Message
	// contains filtered or unexported fields
}

EFQM represents a connection

func NewEFMQ

func NewEFMQ(networkInterface string) (*EFMQ, error)

NewEFMQ is a factory function to create a value of EFMQ type

func (*EFMQ) Listen

func (mq *EFMQ) Listen()

Listen announces the subscriptions to which we are subscribed and then starts listener func in goroutine

func (*EFMQ) Publish

func (mq *EFMQ) Publish(topic string, payload string) error

Publish broadcasts a message on the network which comprises topic and payload

func (*EFMQ) Subscribe

func (mq *EFMQ) Subscribe(topic string)

Subscribe takes a new subscription and stores it to slice

func (*EFMQ) Subscriptions

func (mq *EFMQ) Subscriptions() []string

Subscriptions returns list of topics currently subscribed to

func (*EFMQ) Unsubscribe

func (mq *EFMQ) Unsubscribe(topic string) error

Unsubscribe removes subscription from slice store

type Message

type Message struct {
	Topic   string `json:"tpc"`
	Payload string `json:"pyld"`
}

Jump to

Keyboard shortcuts

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