User Agents

Web browsers and everyday web tech, explained

Connect Rails to Browserless Chrome: Step-by-Step

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Connecting a Rails app to a Browserless Chrome container lets you run headless Chrome tasks—like generating PDFs, taking screenshots, or scraping JavaScript-heavy pages—without managing Chrome yourself. You send HTTP requests to a Dockerized Browserless service, and it returns the results. This guide walks you through setting up the container, configuring your Rails app, and making your first call.

What You'll Need

  • A Rails app (any version, but 6+ recommended) with internet access.
  • Docker installed on your development machine or server.
  • Basic familiarity with Rails routes, controllers, and environment variables.
  • An HTTP client gem like net/http (built-in) or httparty (optional).

Step-by-Step

  1. Pull and run the Browserless Chrome Docker image.
    Open a terminal and run:
    docker run -d -p 3000:3000 browserless/chrome
    This starts a container with Browserless listening on port 3000. You can change the host port (e.g., -p 8080:3000) if 3000 conflicts with your Rails server.
  2. Verify the container is running.
    Run docker ps to see the container status. Then test the API by visiting http://localhost:3000/json/version in a browser—you should see a JSON response with Chrome version info.
  3. Add a gem for HTTP requests (optional).
    If you prefer a clean syntax, add gem 'httparty' to your Gemfile, then run bundle install. Otherwise, use Ruby's built-in net/http.
  4. Create a service object in Rails.
    Create app/services/browserless_client.rb with a method to send requests. For example:
    class BrowserlessClient
      def self.screenshot(url, options = {})
        query = { url: url }.merge(options).to_query
        uri = URI("http://localhost:3000/screenshot?#{query}")
        Net::HTTP.get_response(uri)
      end
    end
  5. Set the Browserless URL via environment variable.
    Don't hardcode localhost. In config/application.rb or an initializer, define:
    BROWSERLESS_URL = ENV.fetch('BROWSERLESS_URL', 'http://localhost:3000')
    Then use BROWSERLESS_URL in your service.
  6. Create a controller action to use the client.
    For example, in app/controllers/screenshots_controller.rb:
    def create
      response = BrowserlessClient.screenshot(params[:url])
      send_data response.body, type: 'image/png', disposition: 'inline'
    end
  7. Add a route.
    In config/routes.rb, add post '/screenshot', to: 'screenshots#create'.
  8. Test the integration.
    Start your Rails server (rails s) and send a POST request to http://localhost:3000/screenshot with a url parameter. You should receive a PNG image in response.

Common Mistakes

  • Port conflict: Rails and Browserless both default to port 3000. Change one of them.
  • Using localhost from a different machine: If your Rails app runs on a remote server, use the container's IP or a Docker network.
  • Not URL-encoding parameters: Always use .to_query or URI.encode_www_form to encode URLs passed as parameters.
  • Ignoring response errors: Browserless returns non-200 status codes for invalid requests. Check response.code before processing the body.

Tips

  • Use Docker Compose for local development: Define both your Rails app and Browserless in a docker-compose.yml to manage them together.
  • Set a token for security: Browserless supports a --token flag. Add it to your Docker run command and pass it as a query parameter to every request.
  • Increase timeout for heavy pages: Browserless has a default timeout of 30 seconds. You can pass timeout as a query parameter (e.g., &timeout=60000).
  • Use the waitFor option: For pages that load content dynamically, pass waitFor with a selector to ensure the element appears before capturing.

Troubleshooting

  • Connection refused: Ensure the container is running and the port is mapped correctly. Run docker ps to confirm.
  • Timeout errors: Increase the timeout in your request or pass a longer timeout parameter to Browserless.
  • Empty or corrupt response: Check the response status code. If it's 400, your URL parameter is likely malformed. If 500, the page might be unreachable or too heavy.
  • Container stops unexpectedly: Check Docker logs with docker logs <container_id> to see error messages.

Frequently Asked Questions

Can I use Browserless without Docker?

Yes, you can install Chrome and run the Browserless Node.js server directly, but Docker is the recommended way because it handles dependencies and isolation.

How do I generate a PDF instead of a screenshot?

Use the /pdf endpoint instead of /screenshot. For example: http://localhost:3000/pdf?url=...

Is it safe to expose Browserless to the public internet?

No. Always run it behind a firewall or use a token. The default setup has no authentication, so anyone could use it to make requests from your server.

Can I use a headless Chrome binary instead of Browserless?

Yes, but Browserless simplifies the process by providing an HTTP API and handling concurrency, so you don't have to manage Chrome processes yourself.

How do I pass custom Chrome flags?

Use the launchOptions parameter in the query string, e.g., &launchOptions[args][]=--no-sandbox.

Does Browserless support authentication?

Yes, you can set a token when starting the container and then include ?token=your-token in every request.