|
| 1 | +// RabbitMQ AMQP 1.0 Go Client: https://github.com/rabbitmq/rabbitmq-amqp-go-client |
| 2 | +// RabbitMQ AMQP 1.0 documentation: https://www.rabbitmq.com/docs/amqp |
| 3 | +// The example is demonstrating how to connect to RabbitMQ using AMQP 1.0 over WebSocket protocol, |
| 4 | +// declare a queue, publish a message to it, and then consume that message. |
| 5 | +// AMQP 1.0 over WebSocket documentation: https://www.rabbitmq.com/blog/2025/04/16/amqp-websocket |
| 6 | +// example path: https://github.com/rabbitmq/rabbitmq-amqp-go-client/tree/main/docs/examples//web_sockets/web_sockets.go |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + |
| 13 | + rmq "github.com/rabbitmq/rabbitmq-amqp-go-client/pkg/rabbitmqamqp" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + const amqpConnectionString = "ws://rabbit:rabbit@127.0.0.1:15678/ws" |
| 18 | + rmq.Info("[Example]", "Starting web socket connection to", amqpConnectionString) |
| 19 | + env := rmq.NewEnvironment(amqpConnectionString, nil) |
| 20 | + conn, err := env.NewConnection(context.Background()) |
| 21 | + if err != nil { |
| 22 | + panic(err) |
| 23 | + } |
| 24 | + _, err = conn.Management().DeclareQueue(context.TODO(), &rmq.QuorumQueueSpecification{ |
| 25 | + Name: "test-ws-queue", |
| 26 | + }) |
| 27 | + if err != nil { |
| 28 | + panic(err) |
| 29 | + } |
| 30 | + // declare new producer |
| 31 | + producer, err := conn.NewPublisher(context.TODO(), &rmq.QueueAddress{ |
| 32 | + Queue: "test-ws-queue", |
| 33 | + }, nil) |
| 34 | + if err != nil { |
| 35 | + panic(err) |
| 36 | + } |
| 37 | + msg := rmq.NewMessage([]byte("Hello over WebSockets")) |
| 38 | + |
| 39 | + publishResult, err := producer.Publish(context.Background(), msg) |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + switch publishResult.Outcome.(type) { |
| 44 | + case *rmq.StateAccepted: |
| 45 | + rmq.Info("[Publisher]", "Message accepted", publishResult.Message.Data[0]) |
| 46 | + default: |
| 47 | + rmq.Warn("[Publisher]", "Message not accepted", publishResult.Message.Data[0]) |
| 48 | + } |
| 49 | + |
| 50 | + // declare new consumer |
| 51 | + consumer, err := conn.NewConsumer(context.TODO(), "test-ws-queue", nil) |
| 52 | + if err != nil { |
| 53 | + panic(err) |
| 54 | + } |
| 55 | + deliveryContext, err := consumer.Receive(context.Background()) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + rmq.Info("[Consumer]", "Message received", string(deliveryContext.Message().GetData())) |
| 60 | + err = deliveryContext.Accept(context.Background()) |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + // clean up |
| 65 | + err = consumer.Close(context.TODO()) |
| 66 | + if err != nil { |
| 67 | + panic(err) |
| 68 | + } |
| 69 | + err = producer.Close(context.TODO()) |
| 70 | + if err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + |
| 74 | + err = conn.Management().DeleteQueue(context.Background(), "test-ws-queue") |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + |
| 79 | + err = conn.Close(context.TODO()) |
| 80 | + if err != nil { |
| 81 | + panic(err) |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments