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
And then execute:
$ bundle install
Or install it yourself with:
$ gem install tamashii
There 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.new
Then, you can start the server with:
If you want to start the server with other web server, such as 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 end
Client , there are four events that need to be handled.
on_open: when the user is connected to the server
on_message: when the server receives the message from user
on_error: when the server gets an error
on_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 end
By 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@example.com:tamashii-io/tamashii.git
Initialize the development environment
Run the spec
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.