This project pulls real-life IoT sensor data from an MQTT broker and displays it to the user with React and HighCharts.
This app was built with the help of my mentors at the Turing School of Software and Design, as well as others in the community.
This Rails API app spins up a connection to an MQTT broker site (Eclipse M2M) on server start. It keeps the connection open using Thread.new
in order to not block the Rails process.
Messages from the broker have a tank name/id and a level/% full datapoint. When new messages arrive, Rails:
- updates the record in the tanks table based on the tank name and level
- sends a background worker to broadcast the new data over the ActionCable socket
- tells ActionCable to broadcast the new data when there are sufficient resources (usually instantaneously).
The data stream can also be accessed by the normal api/v1/etc. system. The current frontend implements this method.
Potential areas of help:
- a better solution than
Thread.new
for concurrency in Rails - a background worker didn't seem correct for keeping a connection open, and EventMachine appears to be somewhat outdated. Right now it works, but if we start to receive large volumes of messages, this solution won't hold. - Testing ActionCable - apparently this isn't really yet a thing, but I'd love ideas.
- Better testing for the MQTT service.
To get set up:
$ git clone [email protected]:katiekeel/capstone-backend.git your-directory
$ cd your-directory
$ bundle
$ rails s
And, in a separate tab:
$ redis-server
Note: you'll need to have Redis installed on your system as well. More information here.
To run the test suite:
$ rspec