Tamashii is a package designed for IoT. You can write the WebSocket server and client easily by using this gem.
Add the following code to your Gemfile
gem 'tamashii'And then execute:
$ bundle installOr install it yourself with:
$ gem install tamashiiThere are two section in Tamashii, Server and Client, is responsible for the WebSocket server and client, respectively.
Server section is a server designed based on Rack, it can not only be easily compatible with the web server, such as Puma, Passenger, but also be used as a module in the Rails and other projects.
To start the server, generate config.ru and add the following code to it.
require 'tamashii/server'
run Tamashii::Server::Base.newThen, you can start the server with:
$ rackup
If you want to start the server with other web server, such as Puma:
$ puma
You can refer to the project in tamashii-manager in the IoT server application.
In Tamashii, we only need to focus on how to connect with WebSocket users to exchange information, on the process of multi-process web server problems have been resolved in Tamashii.
We can create a Client object to handle the behavior of each user.
Tamashii::Server.config do |config|
config.connection_class = Client
endIn Client , there are four events that need to be handled.
on_open: when the user is connected to the serveron_message: when the server receives the message from useron_error: when the server gets an erroron_close: when the connection is shut down
In most cases, we only need to deal with parts of on_message , the other events can be handled as needed.
class Client
def on_message(data)
# Processing for the received Data (Binary)
end
endBy default Tamashii will broadcast the received message to other clients automatically.
The client is in another repository: tamashii-client
To get the source code
$ git clone [email protected]:tamashii-io/tamashii.git
Initialize the development environment
$ ./bin/setup
Run the spec
$ rspec
Installation the version of development on localhost
$ bundle exec rake install
Please report to us on GitHub if there is any bug or suggested modified.
The project was developed by 5xruby Inc.