/mqtt-client

Primary LanguageGoMIT LicenseMIT

Go Report Card License: MIT

mqtt-client

An mqtt client in go supporting mqtt v3.1.1. Mostly for learning bit operations, not for production use.

mqtt 3.1.1 feature support

  • connect
  • ping
  • subscribe
  • disconnect
  • publish
  • QoS
  • session

design goals

  • Composition over inheritance
  • Simplicity: just packets and a stream
  • Idiomatic go
  • Decent test coverage

notes

  • Use printf "%d\n" <hex> and printf "%x\n" <int> to convert between integer and hex
  • Use <-func() to indicate waiting
  • len(chan) only shows unread items, not pending writers
  • SIGINT takes a while since DISCONNECT is also time sharing the stream
  • Use Set*Deadline to cancel net.Conn.Read
  • Use context instead of closing a channel to abort reading a channel if you have multiple writers
  • Empty channels do not need to be closed. They will be garbage collected.
  • Stopping a time.Timer does not close time.Timer.C so stopping a timer while we're reading from it in another goroutine will leave the channel open. Will this leak that goroutine? Or will gb reap it?

usage

$ make test
$ make run

todos

  • use https://github.com/karlpokus/mqtt-parser for monitoring connection
  • tests
  • parse connack return codes
  • verify pingresp after sending pingreq
  • handle conn errors https://github.com/karlpokus/broker/blob/master/pkg/broker/broker.go
  • try bytes.Buffer and buf.WriteTo(conn)
  • close connection on disconnect
  • rw wrapper that log op codes
  • time share rw
  • try buffered rwc - no
  • stream.Fake
  • rw error handling options: pass fatal or return err
  • move opFuncs from packet to stream
  • type op execution timeout
  • implement read timeout for io.ReadWriter
  • client ui
  • client config
  • client topic router
  • prefix log records
  • stop ops listener before exit
  • pass error in Response
  • ack pending queue
  • maybe rename type op to tx
  • good comments on packet bytes
  • expose Response.Message and Topic
  • *ack name in timeout error
  • not before SUBACK popped should we notify client
  • ack name
  • reconnect option
  • make op funcs cancelable
  • generate packet id
  • client initiated disconnect
  • convert packet.Packet to func(b)