🌈 ESP32-S3 Rainbow: ZX Spectrum Emulator Board! Get it on Crowd Supply →
View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Action Cable debuted at RailsConf 2015 and is now part of Rails 5. Built on top of WebSockets it provides real time communication with your backend server.

Part 1 of the videos is here:

And Part 2 here:

I’ve created a very simple Rails5 chat application that you can download from here.

You can run this locally using the following command:

rails s

Open a couple of browser windows and you should be able to talk to yourself…

As this is a new application we need to initialise Elastic Beanstalk and select the environment that we configures in the previous steps.

eb init

Before deploying we need to make some tweaks to our environment. Currently our load Balancer is setup to use HTTP and HTTPS - it won’t currently understand the Web Socket protocol.

We need to change this so that it uses raw TCP and SSL:

Add domains names to your certificate

The next thing we need to do is modify the configuration of the nginx instance that is sitting in front of our Rails application on the Elastic Beanstalk instances.

If we were manually configuring a server we’d simply SSH onto our machine and edit the nginx config files. This doesn’t work for PAAS type deployments where the machines you are running on are ephemeral.

To configure our nginx server we need to add a new folder to the root of our project .ebextensions and add a config file nginx.config.

files:
  /etc/nginx/conf.d/proxy.conf:
    content: |
      client_max_body_size 500M;
      server_names_hash_bucket_size 128;

      upstream backend {
        server unix:///var/run/puma/my_app.sock;
      }

      server {
        listen 80;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        server_name *.cmgresearch.net;

        large_client_header_buffers 8 32k;

        location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;

          proxy_buffers 8 32k;
          proxy_buffer_size 64k;

          proxy_pass http://backend;
          proxy_redirect off;

          location /assets {
            root /var/app/current/public;
          }

          # enables WS support
          location /cable {
            proxy_pass http://backend;
            proxy_http_version 1.1;
            proxy_set_header Upgrade "websocket";
            proxy_set_header Connection "Upgrade";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          }
        }
      }


container_commands:
  01restart_nginx:
    command: "service nginx restart"

This config file will create a new file on our Elastic Beanstalk instances called /etc/nginx/conf.d/proxy.conf and will then restart the nginx server so that it picks up the new config file.

The important part this new file is:

# enables WS support
location /cable {
  proxy_pass http://backend;
  proxy_http_version 1.1;
  proxy_set_header Upgrade "websocket";
  proxy_set_header Connection "Upgrade";
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Using PostgreSQL

Initially we setup ActionCable to use PostgreSQL as it’s back end by setting up the

Switch back to redis by editing config/cable.yml as follows:

production:
  adapter: postgresql

This will use our existing DATABASE_URL to connect to Postgres and then use Postgres’ built in NOTIFY and LISTEN commands

Make sure all our changes are committed and deploy the application:

git add .
git commit -m "Chat app with postgres"
eb deploy --profile demo

And you should have a chat application running on your instance.

If you hit any issues with the websocket not connecting then you can use:

eb logs

to view the logs from your server - this should flag up any errors with the ActionCable system.

Using Redis

Now we know that our chat application is working in Elastic Beanstalk we can switch over to using Redis. Lets change our cable.yml file so in production it uses Redic:

production:
  adapter: redis
  url: <%= ENV['REDIS_URL'] %>  
  channel_prefix: <%= ENV['CABLE_CHANNEL_PREFIX'] %>

We now need to do some work in our VPC to create a new security group for our Redis instances and set it up so that our web server security group can access the Redis port:

Redis Security Group

Now navigate to the ElastiCache dashboard and we can create a new Redis instance. Make sure you expand out the Advanced settings and create a new Private Subnet group with the private subnets from your VPC. This is where our convention for our subnet CIDR blocks really helps as Amazon doesn’t show us any of our names.

Create private subnet

Finally make sure that you place the Redis instances in our Redis security group so that it is accessible from our Web Server instances.

Redis Security Group

The last thing we need to do is add the two environment variables (REDIS_URL and CABLE_CHANNEL_PREFIX) to our Elastic Beanstalk settings.

Set CABLE_CHANNEL_PREFIX Set REDIS_URL

Once that cofiguration has completed we can deploy and test our application again:

git add .
git commit -m "Switch to Redis"
eb deploy --profile demo

All being well our chat application should still work, but now it will be using Redis as the ActionCable back end instead of PostgreSQL.

Once again, if you have issues then run check the logs as it normally tells you exactly what has gone wrong:

eb logs

Related Posts

Step 2 - Setup Elastic Beanstalk: Deploying a Rails Application to Elastic Beanstalk - In this post, I describe how to set up, configure and deploy an Elastic Beanstalk application on a VPC in Amazon AWS, using Rails 5 and Ruby, using Puma for deployment and configuration of Public and Private subnets in Elastic Load Balancer. I also cover the details of network card settings, the selection process for subnets and security groups for Load Balancer and Instances, and finally shared the result of deploying the sample application on Elastic Beanstalk.
Step 4: Deploy Rails App To Elastic Beanstalk from Command Line - This post guides you through the essential steps of creating and deploying a Rails application into Elastic Beanstalk environment. Topics include creating an AWS user, configuring AWS CLI for deployment, and setting up the environment variables for your app. Also, it provides you the valuable knowledge of connecting your Rails app to RDS instance and ensuring it successfully connects with the dev database.
Step 1 - Setup VPC: Deploying a Rails Application to Elastic Beanstalk - In this blog post, I am guiding you through the process of deploying a Rails application to Elastic Beanstalk in a Virtual Private Cloud (VPC) on Amazon AWS. I detail the setup of a VPC, subnets, and internet gateways, as well as the configuration of NAT gateways and security groups. Ultimately, this will allow for a safe, internet-accessible environment for your application and its accompanying databases.
Step 8: Set up Active Job on Elastic Beanstalk - This blog post walks through setting up a worker environment on Elastic Beanstalk and using SQS as our Active Job queue, using the active-elastic-job Gem. We start by creating a new queue in our AWS account via Simple Queue Service. Then, we provide our Elastic Beanstalk environments access to the queue and add an AWS_REGION environment variable. We proceed to creating and configuring our worker environment. In our Rails app, we instruct Active Job to use our active-elastic-job queue adapter and create an Active Job. Finally, we deploy our changes to both the web and worker environments, ensuring our ActiveJobs run successfully.
Step 5: Use CircleCI to Deploy To Elastic Beanstalk - In this blog post, we're going to set up an automated deployment pipeline using CircleCI, GitHub, and AWS Elastic Beanstalk. We'll begin by creating a GitHub repository for our Rails application. Next, we're going to help CircleCI understand our build environment by creating a `circle.yml` file to install the AWS EB CLI tools and to define commands for deploying our application. Lastly, we'll set up the required AWS credentials in CircleCI. Once completed, any changes pushed to the develop or master branch in GitHub will trigger a deployment to the respective environment in Elastic Beanstalk.

Related Videos

Forget the Arduino Serial Plotter - try this instead! - Hey everyone! So, you're probably familiar with the Arduino serial plotter, right? It's useful for basic stuff, but I wanted something better, more cross-platform, and runs right in the browser. Enter the web serial plotter, using the web serial API! It's super flexible with zoom, export options, and light/dark mode. You can even pan through history or switch to absolute time. Plus, it's all secure and runs locally. It's open source, and I'd love your contributions or bug reports on GitHub. It's still a work in progress, but I think you'll find it pretty handy!
ESP-IDF 6 Setup in VS Code: Blink & Debug an ESP32-S3 - Time to get with the program and jump to ESP-IDF v6.0.1 in VS Code. I show you how to install the prerequisites (I’m on a Mac with Homebrew), run the ESP-IDF Installation Manager, spin up a Blink example for an ESP32-S3, tweak menuconfig (GPIO21, 500 ms), then build and flash—switching from stubborn USB JTAG to trusty UART when things fail. We check the monitor, bump CPU to 240 MHz, fix flash to 16 MB, peek at PSRAM, and give PCBWay a quick shout-out.
ESP32-S3 - Which Pins Are Safe To Use? - In this video, I've decided to dive deep into the ESP32-S3, a module ruling my lab recently due to its plug-in-and-play functionality, and the flexibility offered by its GPIO matrix. However, working with it requires vigilance, especially with regard to the strapping pins and USB data pins, among others. Discovering such quirks, I've encountered unexpected values, short glitches and the occasional code crash. To help you avoid these bumps, I've documented everything I've learned on my GitHub repo, where I'm inviting you, my fellow makers and engineers, to contribute your valuable experiences and findings. After a minor hiccup with my ESP32-TV, expect an updated PCB design, courtesy of PCBWay. Explore the ESP32-S3 with me, and let's unravel its secrets together, one pull request at a time.
ESP32 - 40 Independent touch pins! - Wow! I'm absolutely thrilled with the results of this video - I've been experimenting with my ESP 320x Spectrum board and its touch Matrix keyboard. Had a bit of trouble with combined button inputs, so I took a deep dive into how touch works and discovered these old, but gold, ICs. After some testing, they turned out to be a perfect solution to my multiplexing problem! I then got a new board design shipped from PCB way - looks awesome and it didn't break the bank. Can't believe how responsive the keyboard is! Got the 'Hello World' program up and running, and even played Manic Miner. The final board's all set - can't wait to get this on sale! And keep watching because I've got some interesting plans for this expansion port!
How Good Are My USB Cables? - In this video, I engage in a deeper exploration of USB testing, encountering a range of trials and tribulations along the way. First up, I attempt to test a couple of USB cables and discuss their data lines' peculiar situatedness. Then, I delve into the assembly of the PCB boards, sourced from PCBWay, which surely turned into a learning voyage than an easy sail. The first version faced challenges of misordered large-frame stencil, unseen connection problems, and even DRC errors in the submission process. Not dwell on that, I redesign and bring to table Version 2, complete with USB connectors with broken-out pins and designed for visual inspection ease. Although this version demonstrated success, soldering difficulties and bad connections persisted. Considering all the hurdles, I decide to let PCBWay handle the assembly for the next version. For future strategies, adding test points and eliminating the lithium-ion charging circuit seemed more practically viable. Peeping into version 3, testing points for all USB cable pins have been added and even an option to break out actual USB connections, all towards ensuring an improved error checking and usefulness of the assembly.
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


Published

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts