Supabase

How to set up Supabase with RabbitMQ for messaging?

Discover how to seamlessly integrate Supabase with RabbitMQ for better messaging. These step-by-step directions will guide you on enhancing real-time communication in your apps.

Developer profile skeleton
a developer thinking

Overview

Integrating Supabase with RabbitMQ for messaging bridges a real-time open-source database with a sturdy messaging broker for event-driven, asynchronous communication. The setup includes configuring Supabase to push specific data changes into RabbitMQ, setting up RabbitMQ to appropriately handle and route these messages, and creating consumers to process the incoming messages. This blend of technologies enables the creation of scalable, responsive applications that manage real-time data updates and intricate messaging workflows effectively.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to set up Supabase with RabbitMQ for messaging?

Prerequisites

  • Make sure you have Supabase up and running.
  • RabbitMQ should be running too.
  • Node.js needs to be installed on your computer.
  • You should know a bit about JavaScript and PostgreSQL.

Step 1: Set Up Supabase Project

  1. Head over to the Supabase website and either sign in or create an account.
  2. Start a new project and jot down the project URL and API keys from the settings.

Step 2: Set Up RabbitMQ

  1. Install RabbitMQ on your server or locally by following the official installation guide.
  2. Start the RabbitMQ service:
    ```sh
    sudo systemctl start rabbitmq-server
    sudo systemctl enable rabbitmq-server
    ```
  3. Check if RabbitMQ is running by going to http://localhost:15672. Use the default login (guest/guest).

Step 3: Create a Supabase Table

  1. Open the Supabase dashboard.
  2. Go to the SQL Editor and create a new table, like this:
    ```sql
    create table messages (
    id serial primary key,
    content text,
    created_at timestamp default now()
    );
    ```
  3. Make sure to turn on Realtime for the messages table by tweaking its settings.

Step 4: Initialize Node.js Project

  1. Make a new directory for your project and go into it:
    ```sh
    mkdir supabase-rabbitmq
    cd supabase-rabbitmq
    ```
  2. Start a new Node.js project:
    ```sh
    npm init -y
    ```
  3. Install the necessary packages:
    ```sh
    npm install @supabase/supabase-js amqplib dotenv
    ```

Step 5: Configure Environment Variables

  1. Create a .env file in the root of your project and add these variables:
    ```
    SUPABASE_URL=your-supabase-url
    SUPABASE_KEY=your-supabase-key
    RABBITMQ_URL=amqp://localhost
    ```

Step 6: Set Up Supabase Client

  1. Create a file called supabaseClient.js and set up Supabase:
    ```javascript
    const { createClient } = require('@supabase/supabase-js');
    require('dotenv').config();

    const supabaseUrl = process.env.SUPABASE_URL;
    const supabaseKey = process.env.SUPABASE_KEY;

    const supabase = createClient(supabaseUrl, supabaseKey);

    module.exports = supabase;
    ```

Step 7: Establish RabbitMQ Connection

  1. Create a file named rabbitmqClient.js to manage the RabbitMQ connection:
    ```javascript
    const amqp = require('amqplib/callback_api');
    require('dotenv').config();

    const rabbitMqUrl = process.env.RABBITMQ_URL;

    amqp.connect(rabbitMqUrl, (error0, connection) => {
    if (error0) {
    throw error0;
    }
    connection.createChannel((error1, channel) => {
    if (error1) {
    throw error1;
    }
    const queue = 'messages_queue';
    channel.assertQueue(queue, { durable: false });

    console.log('Waiting for messages in %s', queue);

    channel.consume(queue, (msg) => {
    console.log('Received:', msg.content.toString());
    // Perform the action based on the received message
    }, { noAck: true });
    });
    });
    ```

Step 8: Listen for Changes in Supabase

  1. Create index.js to listen for changes in the Supabase table and publish to RabbitMQ:
    ```javascript
    const supabase = require('./supabaseClient');
    const amqp = require('amqplib/callback_api');
    const QUEUE = 'messages_queue';

    require('dotenv').config();
    const rabbitMqUrl = process.env.RABBITMQ_URL;

    amqp.connect(rabbitMqUrl, (error0, connection) => {
    if (error0) {
    throw error0;
    }
    connection.createChannel((error1, channel) => {
    if (error1) {
    throw error1;
    }

    supabase
    .from('messages')
    .on('INSERT', (payload) => {
    const msg = JSON.stringify(payload.new);
    channel.sendToQueue(QUEUE, Buffer.from(msg));
    console.log('Sent to RabbitMQ:', msg);
    })
    .subscribe();
    });
    });
    ```

To run the project:

node index.js

This setup will listen for new messages in the Supabase messages table and publish them to the RabbitMQ queue. The RabbitMQ consumer will then receive and log these messages.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.