Typical way of hosting a Ruby WebSocket service that uses gems? - ruby

While developing JavaScript apps, I usually create an API app, totally separate from the UI app. For the API, I usually use Sinatra.
I'm developing a JavaScript app that will use a WebSocket service I build. I'd like to use Ruby (em-websocket for now) and ActiveModel for data models. I want to keep this really lightweight, like a Sinatra app is for a RESTful API.
It seems my WebSocket service will simply be a ruby script invoked via "ruby web_socket_service.rb". I'd like to be able to use various gems (like activerecord, capistrano, and nokogiri) with this WebSocket service. What's the most typical way of accomplishing this?
Would I be better off creating a standalone gem to contain my models and the WebSocket service script and then host my WebSocket service from that? Or maybe simply include the gems and models directly in the script via "gem 'name'? Or, is there some special library or framework commonly-used to tackle this?

Look at a Rails app. That's the approach I would take if your WebSocket service starts to grow towards a medium-sized app. I.e. bin, lib, Rakefile, and a Gemfile for your gems and bundler.
For smaller apps you can still use a Gemfile and bundler to manage the included gems. This locks gem versions so you won't have conflicts if you deploy to other servers. And then just put everything into one or two script files, similar to Sinatra.
Creating standalone gems is really only useful for libraries or application parts that are reusable across many applications. This doesn't sound like that sort of thing.

Related

Using Hanami model and rake tasks without a router etc

I'm going to write a service that will using amqp protocol, without http at all. I like hanami's paradigm of repository-entity-model-interactors and I wonder to use those in my project. Generating all that stuff by hand, sure, is boring.
So, I wonder to grab rake tasks. Looking into config/environment etc, ughhhh. What is the best method, shortly, to use those tools without hanami router and controllers? Or, it is all integrated tightly?
As I think for that moment, there are two ways:
a) To include only hanami-model into my Gemfile, then copy by hand every needed file from gem hanami.
b) To create hanami project and do not use rackup.
I'm disappointed.
Alternatively, you can add hanami as a development gem. That gives you access to the code generators. At the deploy stage, you don't bundle hanami, so the app will only have hanami-model and hanami-utils in production.
hello. If I understand you right, you want to use interactors only with models. Interactors you can use as a regular ruby library.
For model, you need to configure all this staff and load to memory. You can check the example from our playbook. Hope it'll be helpful for you
https://github.com/hanami/playbook/blob/master/development/bug_templates/model_psql.rb

How to use activerecord alone?

I prepare to develop one project, which has no UI. The project just need to interact with database, so is there any example for reference ?
Does your app have ties to a Rails-based app and need to read the Rails configuration files? Or, is it entirely stand-alone and have no Rails interaction?
ActiveRecord is OK for that, but if I don't need Rails compatibility I use Sequel. It's a great ORM that I find to be much more flexible.
If you need Rails compatibility and want to use ActiveRecord, look into using rails runner. From the docs:
runner runs Ruby code in the context of Rails non-interactively. For instance:
$ rails runner "Model.long_running_method"
Rails runner is for command-line apps that don't need the HTTPd server or user-interface of Rails. I use them for things like an app that runs daily to ftp files from a site for analysis. It has to write to the database, so it has access to all the models I've defined, but it never needs to present anything to the user since it is invisible to them.

Ruby Frameworks - Request Entry Point

I'm learning ruby and looking at his frameworks. One things that i can't understand is how frameworks handles requests in ruby world. Digging deeper I found that there's a middleware called Rack that does the job.
So my questions are:
How does Rack handles HTTP request?
Coming from PHP where there's the famous "index.php" file as a entry-point, which is the corrispective in the ruby world?
Thanks in advance.
From the rack specification:
A Rack application is an Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns an Array of exactly three values: The status, the headers, and the body.
In pratice, the common way that a rack application is started is defined in a config.ru file. If you look in the base directory of a rails app you will see it. In rails, it includes config/environment.rb, which includes config/application.rb, which includes boot.rb, which includes gems and whatnot. From that point the framework starts to do its thing.
The general idea with rails is that a dispatcher takes any request and decides what needs to be done with it. The dispatcher can be seen as an equivalent to the index.php you mentioned.
How the config.ru file gets accessed (or how the rack app is started) is dependent on the way your application is deployed. Phusion Passenger, a popular module for apache and nginx, will look for config.ru in the root directory of any application you have added to the server config file.
It really comes down to the deployment option. Ruby apps can be run via apache/nginx modules, directly via web servers written in ruby, and via CGI.
Here is a description of the different ways web apps are deployed, from the passenger docs

Ruby (off the Rails) Hosting

Many people have asked about Rails hosting on this site, but I'm not familiar enough with the back end of things to know if there's a difference.
I want to host some Ruby CGI 'webservices', basically just ruby methods that take parameters from a POST request, access a MySQL db and return data.
I've looked at RoR and it seems like overkill for this, from what I can tell it's for speeding up the development of data baesd CRUD sites, which is not at all what I'm doing.
So my question is, does this affect the hosting provider I choose? Does anyone recommend a good Ruby host for CGI operations? I'm not familiar with FastCGI, mod_ruby, Passenger, Mongrel etc. and what they mean for performance, scalability etc. I just want to host my ruby scripts with reasonably good performance, and all the info out there(and here) seems to be focused on rails.
First, if you want lightweight, Sinatra is usually my first pick. Pair it up with rack and Passenger for best results. It's not CGI, but realistically speaking, CGI is rarely a good match-up with Ruby.
Here's the "Hello World!" Sinatra app from the main page:
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end
Hard to get more lightweight than that.
As for providers, anybody that supports Passenger (mod_rack) should be able to handle Sinatra. I'm a big fan of Slicehost personally, but they're a VPS host, which means you need to be able to install and manage the entire stack yourself. If you don't mind paying a tiny bit extra for the infrastructure, Heroku makes installation and deployment dead simple, so long as your needs don't exceed what they provide (sounds like they won't). In the unlikely event that you're only using 5MB or if you're using an external storage mechanism like Amazon RDS, Heroku may actually be free for you.
Update:
Passenger is an Apache module that allows Rack applications to be run inside of Apache.
Rack is a middleware layer that separates the web server and the web framework from each other. This allows web frameworks to run on any web server for which there is an adapter.
Sinatra is a lightweight web framework that runs on top of Rack.
Once Passenger and Rack are installed (gem install rack, gem install passenger) you just need to edit the Apache vhost to point at the config.ru file for your Sinatra app and create the required directories as per the Passenger docs and you'll be good to go.
I think you might want to look into Rack. It allows you to do the kinds of things you're talking about and shrugs off the weight of frameworks like Rails or Merb. Rack applications can be hosted at a place like Heroku.

Ruby Web Services

I'm contemplating creating a web application using a Ruby on Rails/MySQL stack and I am wondering what capabilities are available around web services and SOAP. Is there a capability within the framework or does it require an extension and if so what?
Rails opted for REST over SOAP:
It’ll probably come as no surprise
that Rails has picked a side in the
SOAP vs REST debate. Unless you
absolutely have to use SOAP for
integration purposes, we strongly
discourage you from doing so. As a
naturally extension of that, we’ve
pulled ActionWebService from the
default bundle. It’s only a gem
install actionwebservice away, but it
sends an important message none the
less.
Still, if you must use SOAP, there's always soap4r, but it only supports SOAP 1.1. A better option might be WSF/Ruby. Mark Thomas has an example controller to help you get up and running.
Rails has opted for REST over SOAP in the default framework. It's not hard to go the SOAP route if you want to, though.
I don't recommend soap4r - as of a few months ago, anyway, it wasn't production quality. The updated version of ActionWebService has worked much better for me.
The old way in Rails was to use the Action Web Service framework. The preferred way now is to use the ActiveResource framework for RESTful web services.

Resources