2016年8月30日 星期二

MQTT 安裝及web socket設定

MQTT:是一個client server的publish/subscribe訊息交換協定

安裝過程:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

sudo apt-key add mosquitto-repo.gpg.key

rm mosquitto-repo.gpg.key

cd /etc/apt/sources.list.d/

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list

sudo apt-get update

sudo apt-get install mosquitto mosquitto-clients
安裝好之後,
其實就會隨著開機啟動MQTT把這台機器也當作Topic Server,
可以做本地端測試

開啟兩個終端機,分別執行下面的1以及2

1.建立Subscriber(訂閱者),訂閱topic名稱為 TEST_TOPIC
mosquitto_sub -d -t TEST_TOPIC

2.建立Publisher(發布者),發佈到topic名稱為TEST_TOPIC
mosquitto_pub -d -t TEST_TOPIC -m "Test message. Test message. Test message."

此時在1號終端機,Subscriber應該會看到來自Publisher的訊息

而2號終端機所key的那行指令,若預設沒有加上-h指定topic server,則會以自身當作topic server以及一個publisher的腳色

※ 如果要指定port的話 使用-p 8888 參數
※ console使用的port 和 websocket的port不同
=============================================================

使用js與MQTT溝通
sudo vim /etc/mosquitto/mosquitto.conf

修改內容:
 bind_address localhost
port 9999  #預設MQTT的port

#下面2行是設定給websockets的port
listener 8888
protocol websockets



設完重開
sudo service mosquitto restart

server部份設定完成
===============================================================


sample code: https://github.com/xms/MQTT-Client-with-web-socket








2016年8月24日 星期三

Install mongoDB on ubuntu 14.04 LTS

MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL, and Microsoft SQL. MongoDB features include: full index support, replication, high availability, and auto-sharding.
Pre-Flight Check
  • These instructions are intended for installing MongoDB on a single Ubuntu 14.04 LTS node.
  • I’ll be working from a Liquid Web Core Managed Ubuntu 14.04 LTS server, and I’ll be logged in as a non-root user, but with sudo access. For information on giving a user sudo access visit our page on How to Add a User and Grant Root Privileges on Ubuntu 14.04.
Step #1: Setup a the Package Database
First we’ll import the MongoDB public key used by the package management system:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Then we’ll create a list file for MongoDB:
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
Now reload the package database:
sudo apt-get update
Step #2: Install Latest Stable Version MongoDB
At this point, installing MongoDB is as simple as running just one command:
sudo apt-get install -y mongodb-org
If you’d like MongoDB to auto-update with apt-get than you’re done with the installation. But, it’s possible to ‘pin’ the version of MongoDB you just installed to prevent apt-get from auto-updating.
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
Step #3: Get MongoDB Running
Start-Up MongoDB
sudo service mongod start
Check MongoDB Service Status
sudo service mongod status
Summary List of Status Statistics (Continuous)
mongostat
Summary List of Status Statistics (5 Rows, Summarized Every 2 Seconds)
mongostat --rowcount 5 2
Enter the MongoDB Command Line
mongo
By default, running this command will look for a MongoDB server listening on port 27017 on the localhost interface.
If you’d like to connect to a MongoDB server running on a different port, then use the –port option. For example, if you wanted to connect to a local MongoDB server listening on port 22222, then you’d issue the following command:
mongo --port 22222
Shutdown MongoDB
sudo service mongod stop
Restart MongoDB
sudo service mongod restart