Skip to content

A PubSub client/server using CoAP protocol in Golang

Notifications You must be signed in to change notification settings

kkdai/CoapPubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PubSub client/server over CoAP(Constrained Application Protocol)

GitHub license GoDoc Build Status

It is a Sub/Pub server and client using CoAP protocol.

Note

It will keep a heart beat signal from client to server if you subscription a topic to remain your UDP port channel.

Install

go get github.com/kkdai/CoapPubsub

Usage

Server side example

Create a 1024 buffer for pub/sub server and listen 5683 (default port for CoAP)

package main

import (
	"log"

	. "github.com/kkdai/CoapPubsub"
)

func main() {
	log.Println("Server start....")
	serv := NewCoapPubsubServer(1024)
	serv.ListenAndServe(":5683")
}

Client side example

Create a client to read input flag to send add/remove subscription to server.

package main

import (
	"flag"
	"fmt"
	"log"

	. "github.com/kkdai/CoapPubsub"
)

func main() {
	flag.Parse()
	if len(flag.Args()) < 3 {
		fmt.Println("Need more arg: cmd topic msg")
		return
	}

	cmd := flag.Arg(0)
	topic := flag.Arg(1)
	msg := flag.Arg(2)

	fmt.Println(cmd, topic, msg)

	client := NewCoapPubsubClient("localhost:5683")
	if client == nil {
		log.Fatalln("Cannot connect to server, please check your setting.")
	}

	if cmd == "ADDSUB" {
		ch, err := client.AddSub(topic)
		log.Println(" ch:", ch, " err=", err)
		log.Println("Got pub from topic:", topic, " pub:", <-ch)
	}
	log.Println("Done")
}

ex: Add subscription on topic "t1"

client ADDSUB t1 msg

ex: Remove subscription on topic "t1"

client REMSUB t1 msg

ex: Publish "mmmmm" to subscription topic "t1"

client PUB t1 mmmmm

TODO

  • Hadle for UDP packet lost condition
  • Gracefully network access

Benchmark

TBD

Inspired

Project52

It is one of my project 52.

License

This package is licensed under MIT license. See LICENSE for details.

About

A PubSub client/server using CoAP protocol in Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages