Sinatra server push? - ruby

What is the best way to push data from a server written in Sinatra to a client? Think similarly to a chat room, but without Ajax polling every 2500ms.
I know of Juggernaut in Rails, but was curious about Sinatra.

A little googling turned up this blog post by Marc-André Cournoyer, which discusses the asynchronous response feature of the Thin web server and includes a link to an async-sinatra library to do just this. You could also take a look at orbited-ruby, a Ruby implementation of the Orbited Comet server. Both of these appear to be pretty new and untested, so you might have to do a little hacking yourselves to get things working correctly for your application.

Related

Are there any web frameworks on top of EventMachine?

Are there any web frameworks on top of EventMachine? So far, I've found Fastr and Cramp. Both seem to be outdated.
Moreover, Googling how to setup Rails + EventMachine, returns a limited amount of results.
NodeJS is really nothing new. Evented I/O has been around for a very long time (Twisted for Python and EventMachine for Ruby). However, what attracts me to NodeJS, is the implementations that are built on top of it.
For example. NodeJS has TowerJS. Among plenty others. Perhaps, this is one of the many contributing reasons to its trending factor.
What I like most about TowerJS, is its Rails-like structure. Is there anything like it for EventMachine?
Goliath is an open source version of the non-blocking (asynchronous) Ruby web server framework.
You may find async sinatra interesting
Besides EventMachine and the others mentioned here, there's vert.x. I'm not sure how much of a "web framework" it is, but its site shows examples for a simple app like one might write in Sinatra.

What does pubsubhubbub mean for a front-end developer?

Google's introductory text to pubsubhubbub describes it as a server-to-server protocol.
Google's video introduction includes an example where a web page is updated very quickly when an entirely separate blog is edited. It is stated that AJAX is in use. (Correct me if I'm wrong but AJAX is only ever going to be a polling technique)
I get the impression that, with the use of pubsubhubbub, although servers might not have to poll each other (because subscribers also run servers hubs can POST to), browsers have to poll with AJAX in the usual way (or use Comet or websockets). Is this correct?
Am right in thinking that from the perspective of the front-end developer, nothing changes? No new techniques are required if pubsubhubbub is being used?
browsers have to poll with AJAX in the usual way (or use Comet or websockets). Is this >correct?
Yes, I believe it has no effect on the browser end of things.
Am right in thinking that from the perspective of the front-end developer, nothing changes?
I think you are!

What aynchronous Ruby server to use?

We're starting development of the new game project using Ruby. We decided to use one of the asynchronous Ruby servers, but we cannot decide which one to choose. Options are:
Goliath
Cramp + thin/rainbows
rack-fiber_pool + rack + thin/rainbows
eventmachine_httpserver
All of them seem to be working with HTTP requests. Cramp also supports Websockets and Server-Side events from the box.
Do you know any pros & cons of these servers?
I've used eventmachine_httpserver to expose a RESTful API in an EventMachine-based IRC bot and would definitely not recommend it for anything serious. It seems more like a minimal proof-of-concept than a serious web server, perhaps best illustrated by the parse_headers hack I had to use.
You may also want to look pub/sub services like nats.

High load RESTful API in Ruby (sync/async implementation)

I'm struggling with implementing a RESTful API that should return JSON response and should sustain very high load.
The highest load will be generated by 'read' part of the API and very little load will be generated by 'write' part of the API.
My first attempt was to write whole API using nodejs. I almost did it but faced very high duplication of models and logic between javascript and ruby, because the API is a part of a bigger system. I tried moving all logic into backend (mySql), but that idea turned out even more uglier.
My second attempt is to write the API in Ruby ecosystem in order to share models/logic and tests between all parts of the system.
I tried using Cramp and Goliath alone, but all that async stuff really complicated API implementation. I only need to have 2 read urls async because they generate the highest load and by going async all the way I was forced to implement the rest of API in async fashion, which didn't add any value.
My current attempt is to go hybrid: use Thin/Sinatra/Cramp cocktail. I'm instantiating Thin rack handle right in Ruby code and using rack builder I'm splitting API between Sinatra, which is taking sync implementation, and Cramp, which is implementing 2 urls in async way.
Is this is a good way to go? Or having Sinatra and Cramp in one web server (Thin) will get me even in more trouble by some reason?
update:
I'm trying solution with sole Sinatra mixed with rack/fiber_pool and em_mysql2. Seems I'm killing two goals - making API async with sync implementation. But I'm suffering from a bug which I think will be fixed quite soon.
Were will be any gotchas going this way?
I don't think it's a good idea to have sync (sinatra) and async (cramp) apps within the same thin process. If the sync part is relatively simple, I'd suggest implementing that in Cramp. A little biased here as I authored Cramp :)
In case you didn't know, Cramp has out of box support for AR/fiber pool - https://github.com/lifo/cramp/blob/master/examples/fibers/long_ar_query.ru
If you decide to use Cramp, I'm happy to help out with any issues as I've been working a lot on cramp recently and am quite pumped up! Just throw me an email!
I'm curious what async stuff you ran into with Goliath? In the common case there should be no async code visible to the end developer.
Is there something we can do better to make this less visible to the end user?

How do comments appear instantly on Facebook?

I was just wondering, How do comments appear instantly on Facebook? For example, when I'm on my profile and my friend comments something on my post, I can instantly see it. Is it AJAX? Or Queuing system? If I want to do the same thing, what do I do?
Thanks
I'm not exactly sure how facebook has implemented their system.
but it will either work with websockets, AJAX or a comet server.
If you want to have the same effect there are a lot of different techniques you could use,
but I would recommend looking into node.js and maybe even the now.js plugging, which allows for realtime updates via websockets. It even has support for older browsers, so if the browser does not support websockets, it will do a fall over to either a comet server implementation, AJAX or an iframe.
Basically websockets allow for better control over when data should be sent or received from and to the server since it constantly listening to the socket, so you only send data when required and same for receiving data as well, where with an AJAX approach you had to make a call every X seconds.
It's extremely easy to setup on a linux environment, and there's ample documentation to get you started.
It works with javascript and is build on the Google V8 engine, so if you've ever worked with OOP Javascript, you should be able to pick it up relatively easy.
LINKS:
http://nodejs.org/
http://nowjs.com/
You'll want to look into PHP sockets
Actually, its long polling according to this answer (which also explains how to verify or see if its changed since the answer):
How does Facebook fetch live updates

Resources