Pages

Monday, March 31, 2014

Getting websocket-rails up and running on Heroku

I wanted to document my endeavors to get websocket-rails up and running on Heroku. In my case, I'll be adding Redis to the mix, as I'm trigger server-side events from a worker process that is running delayed_job tasks. Thus, in a multi-process environment, Redis has to be used to communicate from the worker process to the Rails process (which actually maintains the web socket infrastructure). My experiences with setting this all up were actually quite pleasant and things just worked without many issues. The documentation for websocket-rails could benefit from elaborating some non-development environment scenarios, but that's a very minor gripe.

Step 1: Get the solution running in development

Pretty straight-forward. You have to get everything working in your development environment. Nothing earth shattering here, but if you plan on triggering server-side events to your JavaScript clients from something like delayed_job, you will need to use Redis. I'm using OS X Mavericks here, so I install tools through Homebrew. Here's how I got Redis up and running in my environment:
  1. Install via Homebrew: brew install redis
  2. Start up Redis from the command line: redis-server /usr/local/etc/redis.conf
If you need to monitor Redis, use the redis-cli: redis-cli monitor Now that we have Redis up and running, get websocket-rails integrated into your application. I won't belabor how to do that--the documentation does a good job of detailing how. I use a channel to communicate from the client-side and the server-side. Another thing to note is that for anything I do on the client that I want an event for coming from the server, I use a correlation ID from the client-side that I can keep track of on the client-side, so when an event from the server-side is received, I can determine whether I'm interested in it because it contains the original correlation ID. Read more here about the correlation ID design pattern.

Step 2: Get the solution running on Heroku

Now that everything works in development, I needed to get the solution out to Heroku.
  1. If you previously installed SSL, you will have to disable it before enabling websockets support. Remove the certificates: heroku certs:remove, then remove SSL support through the Dashboard.
  2. Enable websockets support: heroku labs:enable websockets
  3. Add a Redis provider Heroku addon to the app if you haven't done that already. Note the Redis connection information.
  4. Update the websocket_rails.rb initializer with the Redis connection information. I had to add some conditional logic to determine which environment I was deployed in. Also import here is that host and port won't typically be enough to connect to a Redis add-on. You'll definitely need a password and probably a username. Those can be passed through the redis options in this initializer. Currently my websocket_rails initializer has some code in it like this:
    
        if ENV["RAILS_ENV"] == 'production'
            config.redis_options = {
                username: 'rediscloud',
                password: 'UHDDBHD&*#$DFkkdfha',
                host: 'pub-redis-88885.us-east-1-3.3.ec2.redisdomecity.com',
                port: '15204'
            }
        else
            config.redis_options = {
                host: 'localhost',
                port: '6379'
            }
        end
    
    
    This will invariably change so I can differentiate between staging and production, but you get the point.
Doing all this I was able to get the websocket-rails stuff to work and have not had any issues with it, even with dynes spinning up and down.

Tuesday, March 18, 2014

Installing pillow on OS X Mavericks with Xcode 5.1

I've been doing some Python and Google App Engine development lately and I hit an issue with clang throwing an error like the following....
cc -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -arch x86_64 -DHAVE_LIBJPEG -DHAVE_LIBZ -I/System/Library/Frameworks/Tcl.framework/Headers -I/System/Library/Frameworks/Tk.framework/Headers -I/usr/local/include/freetype2 -IlibImaging -I/System/Library/Frameworks/Python.framework/Versions/2.7/include -I/usr/local/include -I/usr/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _imaging.c -o build/temp.macosx-10.9-intel-2.7/_imaging.o

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

error: command 'cc' failed with exit status 1

The following discussion helped: https://stackoverflow.com/questions/22334776/installing-pillow-pil-on-mavericks I've had more issues with the March 2014 version of the Command Line Tools for Mavericks. Grrrr.