Any success with Roda working together with EventMachine WebSockets? - ruby

I have been using roda for sometime now and I would like to add some realtime features to my web-app by pushing the data via websockets.
I have successfully used the gem 'em-websocket' on its own, but have not been able to write one ruby file that has a roda framework AND a web-socket server.
I've tried spinning the run! or start! methods off in separate threads with no success.
Has anyone gotten this to work?

Related

Can I use rack-test for deployment tests?

I've built a simple api in Sinatra, with the purpose of setting up a deployment pipeline using Docker. I'm at a stage where I could easily switch Sinatra for something else and there's currently one reason why I might do so.
I once wrote an api using Express and it was trivial to reuse the tests to test a deployment:
# Testing the code
chai.request(app)
.get('/')
# Testing a deployment
chai.request('http://localhost:8080')
.get('/')
Examples from: https://github.com/chaijs/chai-http#integration-testing
Now I am wondering if I can accomplish the same with rack-test and Sinatra. Simply sending a URL, instead of the app, crashes. So is there an easy way to accomplish this? I suppose I could write a testing framework on top of rack-test, but I'm not sure it's worth it, even though I do prefer Ruby over Javascript and Sinatra over Express.
I realised that I should be able to write a rack app that forwards all requests to the environment I want to run deployment tests against. So I went to Google and found a gem that does just that: rack-proxy
Here's how to write a simple rack app that redirects requests to your server:
require 'rack/proxy'
class Foo < Rack::Proxy
def rewrite_env(env)
env["HTTP_HOST"] = "api.example.com"
env
end
end
Then I ran my tests against Foo.new they succeeded. I checked the logs of that environment and I can confirm that my tests were in fact running against that environment.
Foo may not be the best name for such a proxy and you may not want the host name hardcoded, but I'm sure you can figure out how to make this work in your project if you need it.

Front end separated from back end project using Sinatra

I am planning to create a website that has front end and back end. I was wondering how the FE communicates with the BE.
I saw a project that uses Xampp to run the front end and sinatra for the back end. It needs to start apache, then the backend is fired using the rackup command. I assume the backend runs under Webrick.
Can someone explain how these two ends communicate with each other? If there is a good tutorial for this, I will appreciate it.
Sinatra is a popular option for API-only backend applications. We use Grape with Sinatra or just Sinatra without any dependency.
I have two app examples that can help you:
https://github.com/katgironpe/sinatra-grape
https://github.com/katgironpe/simple-sinatra-mvc
Webrick or Puma can be used with Sinatra, but it's not impossible to run a Ruby app on XAMPP. I did that several years ago. The front-end is probably just consuming the Sinatra API.
You can use Ember CLI project. It can get complex with other options like Angular.js and React.js. Or if you like, just use jQuery.

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

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.

CGI Programming + Ruby

Is there a framework for Ruby for CGI that provides similar functionality as Ruby on Rails (mvc)?
Also, The server where the app shall be used on does not support FCGI, only plain old CGI.
Ruby comes with a CGI module, but it isn't a MVC at all. It makes it easy to extract parameters from a HTTP request passed to the app, encode and decode the query params, etc. It relies on a web server to handle routing the request to the right page, so there's quite a gap between a MVC and a CGI.
There are alternate MVCs for Ruby. Sinatra is very easy to use, and Padrino is built on Sinatra, putting it between Sinatra and Rails. I like using Sinatra at work because it's good for fast prototyping and in-house loads are nowhere close to what we'd get on an internet facing app.
As far as the server not supporting FCGI, a MVC doesn't really care. Put its server on a different port, then reference that port when you want something to talk to Sinatra. For instance, if you tell Sinatra to use 8088, your URLs for Sinatra served pages would be something like: http://host.com:8808/url/path/to/object. Load your Sinatra based app on the web server and start it up. It'll run concurrently with the normal web server.

Integrating Ruby Handler With Apache

If you use Webrick you can implement a servlet and service http requests using ruby. Now I have code that does everything that I want but I would like to move to Apache. Is there a way to modify the .htaccess file to send all the requests through a ruby handler?
you should check out phusion passenger. It lets you easily deploy any ruby Rack based web app in apache. You can then use rails, sinatra, waves, etc from apache.
You're asking for mod_ruby.
That was abandoned years ago in favor of running the Ruby code in a separate process under Mongrel, and just using Apache mod_proxy (or another web server acting as a proxy).
And now that Passenger is available lots of people (including myself) are adopting it instead of Mongrel, because it's simpler.
Just accept that you did it wrong the first time and bite the bullet. Rewrite your code as a Rack service, and run it via Passenger.

Resources