Realtime Applications, is there another way?

Anonymous (not verified) -
6
5
profile

Anonymous (not verified)

Please Register or Login to post a reply

5 Replies

J Jon Tara

This is all well and good for an app written in Javascript... For those of us doing the bulk of our work in Ruby, though, any plans to introduce a WebSocket API in Ruby?

I'd imagine there are any number of RubyGems that do the job, that might be adapted to the Rhodes Ruby environment. One limitation, though, is that Rhodes own network API doesn't provide any means for working with a streaming socket - only HTTP/S.

One can use the Ruby standard library methods for this, but for an encrypted connection, you have to fall back on the venerable (and buggy) OpenSSL. I would much rather see some support for non-HTTP and streaming in the Rhodes network API, because the Rhodes network API uses the native platform's SSL implementation.

It is possible, though, and quite some time ago I implemented secure XMPP (which has similar requirements) in Rhodes.

J Jon Tara

Looked at the code a comments a bit more. Yea, the Rhodes API is PubNub-specific and not really WebSocket. For subscribe,  It "subscribes" by connecting via HTTP and blocks (thus the need for the Ruby thread) waiting for a response. When it gets a response, then it makes another request, again blocking waiting for response. As well, when the connection times out (which it will, at least on mobile networks).

So, it's long-polling, rather than an actual websocket.

That's OK, as it really is well-suited to the mobile environment. It's similar to the BOSH that I use for XMPP in Rhodes.

So, if being tied to PubNub is OK, this will work, and does give you interoperability with real WebSocket clients.

The code is small enough that if I actually used this, I would just re-write it to use Rhodes Network calls. Actually, that solves the thread problem as well as you could just use a Network lambda callback, and Network doesn't care about Ruby thread support since it uses native threads internally.

J Jon Tara

Thanks for the PubNub link! I have a potential use, and so I'll investigate this further.

On first glance, though, I see a couple of problems with the Rhodes client.

1. It uses a Ruby thread. It will work on iOS, then. But Ruby threads still aren't implemented on Android in Rhodes. (I don't know about other platforms.)

2. Not sure it's really using a WebSocket. I looked at the code, which is suspiciously short. It's using HTTP via net/http. Maybe it uses some long-polling HTTP fallback, then. Any such fallback would, of course, be specific to PubNub. I do see that they offer fallback mechanisms.

3. I notice that a line of code is commented-out for SSL support. Since it uses net/http, the only way (as written) to get SSL is as I feared with OpenSSL. That is an option with Rhodes, but not a really attractive one.

4. I didn't look at the code closely enough to see if it re-establishes the long-poll. This would be a necessity on mobile networks, which typically limit the length of HTTP sessions to no more than 2 minutes, often less.

Now, the GOOD news... it is a small amount of code, and uses HTTP. It would be easy to modify it to use the Rhodes Network API instead of net/http. That would get SSL via native device SSL support.

Also, I assume that PubNub offers native libraries for iOS and Android. This would bring real WebSockets. It would mean writing a new commonAPI native extension over the native libraries. That IMO would be the RIGHT way to implement WebSocket for Rhodes.

In the mean time, this is still an intriguing option for those doing their Rhodes development in Javascript. And you could also just do the (real) WebSocket connection in Javascript, and then Ajax received messages back to a Ruby controller. A bit topsy-turvey, and a bit at the mercy of UI demands, but should do in a pinch.

J Jake Bernstein

Thanks for the info Jon,

It helps to have your perspective on the code since I haven't tested the other PubNub SDKs and saves a lot of time!

J Jake Bernstein

Hey Jon,

PubNub has a list of over 70+ SDKs<a href="http://www.pubnub.com/developers/">http://www.pubnub.com/developers/</a…;

They even have an SDK specifically for RhoMobile <a href="https://github.com/pubnub/pubnub-api/tree/master/ruby-rhomobile">pubnub… at master · pubnub/pubnub-api · GitHub</a>
I have not tested them yet but have high hopes based on the quality of PubNubs code I have worked with. Let me know your experience if you decide to work with any of the SDKs

Thanks for the question and I hope this helps,
Jake