Skip to content

Instantly share code, notes, and snippets.

@MirzaLeka
Last active April 23, 2025 19:39
Show Gist options
  • Save MirzaLeka/aed54773a74e6027791bed816dfeb4e5 to your computer and use it in GitHub Desktop.
Save MirzaLeka/aed54773a74e6027791bed816dfeb4e5 to your computer and use it in GitHub Desktop.
.NET to Node.js Rabbit MQ Example

.NET to Node.js via Rabbit MQ

  • Sender: .NET 8
  • Receiver: Node.js
  • Protocol: AMQPS (secured AMQP)
  • RabbitMQ Host:: AWS (AMQ)

Package Versions

.NET

  • .NET: 8
  • RabbitMQ Client: 6.8.1

Node.js

  • Node.js: 20.17.0
  • amqplib: 0.10.7
const amqplib = require('amqplib/callback_api');
const url = "amqps://<username>:<password>@<same-amq-uri-as-sender>";
const queueName = "<same-queue-name-as-sender>;
console.log('Listening...');
amqplib.connect(url, (err, conn) => {
if (err) throw err;
conn.createChannel((err, ch2) => {
if (err) throw err;
ch2.assertQueue(queueName);
// receives messages from the sender
ch2.consume(queueName, (msg) => {
if (msg !== null) {
console.log(msg.content.toString());
ch2.ack(msg);
} else {
console.log('Consumer cancelled by server');
}
});
});
});
using System.Text;
using System.Text.Json;
namespace RabbitMQ_CSharp
{
internal class Program
{
const string AMQP_URL = "amqps://<username>:<password>@<your-amq-uri>";
static async Task Main(string[] args)
{
const string queueName = "<any-queue-name>";
Console.Clear();
var connectionFactory = new RabbitMQ.Client.ConnectionFactory()
{
Uri = new Uri(AMQP_URL),
AutomaticRecoveryEnabled = true
};
Console.WriteLine("Started!");
var connection = connectionFactory.CreateConnection();
var channel = connection.CreateModel();
var g = new Game { Id = 1, Name = "Splinter Cell" };
var serialized = JsonSerializer.Serialize(g);
byte[] bytes = Encoding.UTF8.GetBytes(serialized);
channel.QueueDeclare(queueName,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
// publishes messages to the queue
channel.BasicPublish(string.Empty, queueName, false, null, bytes);
Console.WriteLine("Sent!");
}
}
class Game
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment