Load balancing with HAProxy for chat app by chat room - ruby-on-rails

I'm making a chat app in Rails and using Node.js. I want to load balance with HAProxy, but it's important that all users in a specific chat room are on the same server. How can I achieve this? I've been searching Google, assuming the intel would be out there because this must be a common issue, but I can't find anything.

I think you should use session cookies, it's well described in this documentation : http://blog.haproxy.com/2012/03/29/load-balancing-affinity-persistence-sticky-sessions-what-you-need-to-know/

Related

Rails + Pubnub Integration Architecture

I am new to Pubnub and real-time application. I wrote some ruby application before and I am currently research an option to integrate pubnub into my rails application. I read pubnub docs already and have an basic architecture in my head but not quite sure if it is good or even feasible. I appreciate any opinions.
In the application, there will be a peer to peer chat window, and one person can talk to any person he wants.
1) Rails will not store any messages, and it will only stores which channels each person is subscribing to. In this case, since it is a peer-to-peer chat, a channel person publishing to is also the channel the person subscribing to. Rails will store all channels informations for all channels. And thats' it.
2) In client side, server will give each current_user the channels him/her subscribing/publishing to and using those channels, current_user can retrieve all history chats from pubnub server for channels he/she subscribes to.
This is what's in my head. Is there a better architecture implementation ? As far as I read in pubnub docs, they don't store channels for each unique user. Also, in terms of security, since everything will happen in the front-end and subscribe/publish key will be exposed to the browser. How secure is it ? Any drawbacks to this implementation ?
Thanks !!
Its very feasible... you can even get more creative after you nail down the basics. Here is a good chat with PubNub overview link:
http://www.pubnub.com/use-cases/chat/
Here you will find links to iOS and JS demo chat implementations.
Just for our Ruby fanatics, here is the link to a chat app purely using our Ruby SDK:
https://github.com/pubnub/ruby/blob/master/examples/translator.rb
The ruby app is pretty cool, as it lets people not only chat, but chat across multiple languages (via Bing translation API).
I'd say cut your teeth on these, and if you need further guidance, just shoot an email to support#pubnub.com, and we'll get you up and running with more tools and tricks specific to your use case.

How to create a server accessible by an iphone app

I a thinking of creating an iPhone/iOS app that would include a feature where one user could create a list of words and then save them to their account on a server. Also (and this is very important), the user could share their list with other users by giving them permission.
So my question is, how can I go about creating such a server? For right now, I have a home computer (running Windows XP that just stores data for my music system) which I can use to host the server. I am also open to the use of other online storage services like Google Drive or Dropbox (I can't remember if Amazon does anything like that). However (and I know this may complicate things a bit), but at least for now, I want/need to stick with free services/options.
Just to recap, the key features that I am looking for are:
create users/accounts (on the server)
eventually I may [try] to incorporate the use of other services to log users in like with their email account, OpenId, etc.
the ability to access (log in to) the server (with credentials) from my app
the ability to send/receive data between the server and my app
the ability to share data between users
I know this is a lot to ask for, but if anyone has any suggestions or can get me going in the right direction, it would be much appreciated.
The basic setup would be as follows:
Backend: Database (MySQL), Web server (Apache), with server side scripting (PHP).
Client: iOS device with developed app.
Communication: use HTTP client/server model, communicating with something like JSON.
This is much the same setup as a web server, but instead of serving html/css/javascript etc the results will be JSON.
As far as implementing specifics such as login in, and sharing data between users, this is purely dependent on your implementation. This is not trivial, and not something that can be easily stated in a single post.
Hope this helps.
You could build your own webservice in PHP, Ruby or Python. If you do so I would recommend building a RESTful webservice (http://en.wikipedia.org/wiki/Representational_state_transfer) and then use RestKit (http://restkit.org/) to handle the data in the iOS app. Especially RestKit's CoreData integration is nice in my opinion.
Another solution would be using a service like Parse (https://parse.com/products/data). The first million or so requests per month are free but after that it could get pricy. I personally have not tried it so I couldn't tell you if it is any good.

iOS forum setup

If I wanted to have a user forum in my app would this be a way to set it up?
MySQL DB for the backend
PHP page for the web service
NSURLConnection to talk and receive from the web service
it would be fairly simple, plain text.
I'm sure there's a gazillion ways to set this up but this is the rout I was thinking.
You're not going to have a user forum in your app, you're going to access a user forum from your app. So any way you want to set up the forum on your server, including the one you suggest, will work fine provided there's a way to connect to it from iOS.
You can use NSURLConnection to access the forum, of course, but that class just manages the actual connection. You might find it easier to use a UIWebView as a mini-browser in your app, and simply point the web view at the appropriate URL.

Rails - how to have the client receive/process messages from the server without AJAX?

This may be sort of a newb-ish question. I know you can do this kind of thing in Node.js pretty easily, but I don't know what it's called and haven't had much luck with Google.
Basically, I am trying to build a simple tic-tac-toe server with Ruby on Rails. Players connect to each other, and moves are recorded and results processed live. If it was just having the user send messages to the server, that would be easily done with AJAX. However, I want to have the client wait and listen for the server to point out that the other player has made a move, and then automatically respond to that. I could do this by pinging the server with AJAX constantly, but there must be a better way. I feel like I'm missing some big technology that I haven't found yet just because I'm not entirely sure how to describe it or what it would be called.
Would I want to have the client connect directly to the server and maintain a live connection? If so, how would I do that? If not, what is the better way to do this? How do online games and stock tickers and streaming services provide their content to the client, and what tools does Rails give me to do something similar?
Check eventmachine in ruby
some links
https://github.com/eventmachine/eventmachine/wiki
http://20bits.com/article/an-eventmachine-tutorial
http://rubysource.com/introduction-to-event-machine
There is a faye and private_pub gem which makes things simpler .
There are railcasts available.
http://railscasts.com/episodes/260-messaging-with-faye
http://railscasts.com/episodes/316-private-pub
I recommend trying private_pub which is built on top on faye which uses eventmachine which can solve most of your questions
the pub/sub model helps to the client to subscribe to its channel
, so you can push updated to the channel which will be eventually passed to the client.
Your question and my answers
Q. Would I want to have the client connect directly to the server and maintain a live connection? If so, how would I do that?
A. You can use private_pub or faye to establish long connections and push data
also Check our later for pusher.com they provide services on commercial basis.
Q.How do online games and stock tickers and streaming services provide their content to the client, and what tools does Rails give me to do something similar?
A. AFAIK everyone uses some sort of push technology. pub/sub model. ruby has some gems available for such requirements, faye private_pub some of them..
For a commercial solution, check out Pusher, it does pretty much exactly what you want:
http://pusher.com/
For open source solutions, check out Faye (a pub/sub messaging server):
http://faye.jcoglan.com/
and some awesome railscasts explaining it:
http://railscasts.com/episodes/260-messaging-with-faye
http://railscasts.com/episodes/316-private-pub

Twitter - Constant search for term

I wonder if anyone can help me, I'm getting a little confused as to
which API to use. If anyone can offer some guidance I would really
appreciate it.
I'm trying to create an website where users can monitor Twitter for
certain hashtags. The site will continually search twitter for any new
updates and store any tweet related to that particular hashtag. This process will run for up to 60 days.
As far as I can gather, my two options are:
Using the Search API
The problem with this API is that if I have a 1000 users all
monitoring different hashtags, I am quickly going to reach my API
limit since I will be making a fair few requests, potentially once
every 2-3 minutes. Is there a way to use oauth in conjunction with the
search API so that the limits are user based and not application
based? That way, the limit will be user specific and I won't have to
worry.
Using the Stream API
I thought this might be a better solution, but it seems you are
limited to how many connections you can have open. The documentation
seems unclear as to how this works... is the connection limit per twitter account
or service ip? For example, if my site had 1000 users each of those users was
monitoring a hashtag, would those 1000 stream api connections be
against my servers ip or would they against the user?
You will want to use the Streaming API. You will open a single connection that will track the terms for all of the users. When users add new terms to track you will restart the stream with the new terms. The single stream will be for a bot Twitter account you create and not your users accounts.

Resources