Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

[Greach 2016] Down The RabbitMQ Hole

RabbitMQ is a great infrastructure software, but there is a not very well known feature that allow us to create WebSockets applications its great STOMP integration.

In this talk I’ll show how we can very easily create a very robust WebSocket application using RabbitMQ, Spring Integration and, of course, Groovy.

  • Login to see the comments

[Greach 2016] Down The RabbitMQ Hole

  1. 1. DOWN THE HOLE @alotor
  2. 2. Alonso Torres @alotor
  3. 3. Spring Integration WEB SOCKETS
  4. 4. Browser /GET 200 OK
  5. 5. SEND X BTW, Your thing SEND Y Browser
  6. 6. SUBSCRIBE X X1 X2 X3 Browser
  7. 7. Web Sockets Web standard for bi-directional communication between the browser and a Web Server ● Non-blocking asynchronous communication ● Push notifications
  8. 8. OK, maybe you already knew this
  9. 9. Asynchronous communication? Pub/sub? But...
  10. 10. ● Robust ● Reliable ● Scalable ● Interoperable (as in microservices?) ● Just fast ● Easy to develop (spoiler alert!) IT’S AWESOME!!
  11. 11. Logic Logic WebSockets ????
  12. 12. How do I transform RabbitMQ in a WebSocket server
  13. 13. rabbitmq-plugins enable rabbitmq_stomp
  14. 14. STOMP ● Simple Text Protocol ● Give semantics to the WebSockets ● For example: ○ CONNECT ○ SEND ○ SUBSCRIBE
  15. 15. STOMP Logic Logic WebSockets
  16. 16. https://github.com/alotor/greach2016-rabbitmq
  17. 17. STOMP WebSockets AMQP Twitter Streaming API
  18. 18. STOMP WebSockets AMQP Twitter Streaming API
  19. 19. @Service class MainService { @Autowired WorkerGateway workerGateway public void startTopic(Map message) { new TwitterWorker(auth, message.topic).fetchTweets { String topic, String json -> def tweet = jsonSerializer.fromJson(json.bytes) workerGateway.broadcastMessage(topic, tweet) } } }
  20. 20. STOMP WebSockets AMQP Twitter Streaming API
  21. 21. STOMP WebSockets AMQP Twitter Streaming API
  22. 22. @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { def brokerRelay = config.enableStompBrokerRelay("/queue", "/topic") // Configure the relay data }
  23. 23. @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry .addEndpoint("/stomp") .setAllowedOrigins("*") // CORS configuration }
  24. 24. STOMP WebSockets AMQP Twitter Streaming API
  25. 25. service-activator jsonSerializer.fromJson transformer request inbound-adapter mainService.startTopic Spring Integration
  26. 26. @Bean public IntegrationFlow amqpInboundGateway( ConnectionFactory connectionFactory, JsonSerializer jsonSerializer, MainService mainService) { IntegrationFlows .from(Amqp.inboundAdapter(connectionFactory, "request")) .transform(jsonSerializer, "fromJson") .handle(mainService, "startTopic") .get(); }
  27. 27. gateway jsonSerializer.toJson transformer topic outbound-adapter workerGateway.broadcastMessage Spring Integration
  28. 28. @MessagingGateway(defaultRequestChannel = "responseChannel") interface WorkerGateway { void broadcastMessage( @Header("routingKey") String topic, @Payload Map message) }
  29. 29. @Service class MainService { @Autowired WorkerGateway workerGateway public void startTopic(Map message) { new TwitterWorker(auth, message.topic).fetchTweets { String topic, String json -> def tweet = jsonSerializer.fromJson(json.bytes) workerGateway.broadcastMessage(topic, tweet) } } }
  30. 30. @Bean public IntegrationFlow amqpOutboundGateway( AmqpTemplate amqpTemplate, JsonSerializer jsonSerializer) { IntegrationFlows .from("responseChannel") .transform(jsonSerializer, "toJson") .handle(Amqp.outboundAdapter(amqpTemplate) .exchangeName("amq.topic") .routingKeyExpression("headers.routingKey")) .get() }
  31. 31. ● Security (JWT, tokens…) ● Infrastructure hell (docker!) The bad news
  32. 32. https://github.com/alotor/greach2016-rabbitmq @alotor Questions?

×