Simple pubsub program to demonstrate MQTT with Go
Mac OS X で mosquitto をインストールして、起動します。
$ brew install mosquitto
$ brew install czmq zeromq
$ echo export PATH='/usr/local/sbin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile
$ mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
1421637760: mosquitto version 1.3.5 (build date 2014-10-27 15:13:47+0000) starting
1421637760: Config loaded from /usr/local/etc/mosquitto/mosquitto.conf.
1421637760: Opening ipv4 listen socket on port 1883.
1421637760: Opening ipv6 listen socket on port 1883.
config.tml
のHostを次のように設定します。
Host = "tcp://test.mosquitto.org"
mosquitto
に付属する mosquitto_sub
と mosquitto_pub
コマンドを別々のターミナルで実行し、簡単な動作をテストしてみます。
まず、subscriber を my/topic
トピックに待機させます。
$ mosquitto_sub -t my/topic
つぎに、hello
メッセージを my/topic
トピックにパブリッシュします。
$ mosquitto_pub -t my/topic -m "hello"
すると、subscriber がメッセージを受け取り標準出力します。
$ mosquitto_sub -t my/topic
hello
go で書いた publisher と subscriber を実行します。
$ mkdir mqtt-client-go
$ echo 'export GOPATH=$(PWD)' > .envrc && direnv allow .
publisher をスタート。
$ go get git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git
$ go run pub.go
Connected to tcp://127.0.0.1:1883
別のターミナルで subscriber をスタート。
$ go run sub.go
Connected to tcp://127.0.0.1:1883
メッセージを pub/sub 。
$ go run pub.go
Connected to tcp://127.0.0.1:1883
hello!
$ go run sub.go
Connected to tcp://127.0.0.1:1883
Received message on topic: NPC158.local
Message: hello!
vim で go を記述するときは、.vimrc
に以下を追加しておくとハードタブのインデントが有効になりはかどります。
au BufNewFile,BufRead *.go set noexpandtab tabstop=8 shiftwidth=8