Last Updated: September 30, 2022
·
626
· dylans

Building Complex Shopify Applications with IronMQ and IronWorker

Shopify store owners are building complex systems behind the scenes. There is a ton of metrics that Shopify provides via its API's, but if you want to start pulling in industry and advertising metrics, run complex lookups, do deep learning... the amount of infrastructure and code you'd need to write to coordinate things can start getting a bit out of control. Using third party services to handle these tasks ends up making a lot of sense.

In this arbitrary example, we're going to utilize a few third party services. The Shopify API will notify us when a new customer has been created, IronMQ and IronWorker will handle the messaging/infrastructure/scaling, and then we'll use some additional services to find out more about this customer. We'll be using Ruby to illustrate our example.

Declare the IronMQ gem in your Gemfile:

gem 'iron_mq'

Within your code, or in a console session, create your new IronMQ push queue

When a message comes into this queue it's going to fire off events, with the message body, to the endpoints you specify. In this example, let's say we want an IronWorker to format the customers information and add it to your database. We'll also post to an HTTP endpoint that will take the customers information and do some market research about their shopping habits. Once this service has the data ready for you, it will send it back to another queue that will format their buying habits and also add it to your database.

require 'iron_mq'
ironmq = IronMQ::Client.new(token: 'xxxxx', project_id: 'xxxxx')

new_customer_queue = {
  push: {
    subscribers: [
      {
        name: 'buying-habits-api',
        url: 'https://xxxxxxxx.com/lookup-buying-habits',
      },
      {
        name: 'iron-worker',
        url: 'ironworker://projectid:token@host:port/process-customer'
      }
    ]
  }
}
ironmq.create_queue(new_customer_queue)

customer_buying_habits_queue = {
  push: {
    subscribers: [
      {
        name: 'iron-worker',
        url: 'ironworker://projectid:token@host:port/process-buying-habits'
      }
    ]
  }
}
ironmq.create_queue(customer_buying_habits_queue)

Start populating your customer queue

The easiest way to populate your queue would be to create a web-hook in Shopify that posts customer data directly to your "new-customer-queue". Since IronMQ has an HTTP API, it's just a matter of supplying Shopify with the correct URL to post to:

POST queues/new-customer-queue/messages

You can also post to your queue via IronMQ's client libraries:

queue = ironmq.queue('new-customer-queue')
queue.post('customer data here')

Conclusion

Using Shopify web-hooks, IronMQ and IronWorker you're able to quickly tie a bunch of services together and push a meaningful aggregation back to your database. If your application starts getting popular, you don't have to worry about managing or scaling any of this infrastructure as it all happens for you. The folks at Shopify can help you get your web-hooks setup and the staff and Iron are happy to help you get up and running quickly.