28
© 2016 Magento, Inc. Page | 1 Integrations with Magento, end to end story: RabbitMQ, APIs

Mage Titans USA 2016 - Eugene Tulika - Integrations with Magento, end to end story: RabbitMQ, APIs

Embed Size (px)

Citation preview

Page 1: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 1

Integrations with Magento, end to end story: RabbitMQ, APIs

Page 2: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 2

Magento 2 Architect:- Magento Service Layer- Composer Integration- Magento CLI- Rabbit MQ Integration

@vrann Eugene Tulika

Page 3: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 3

On average, Magento store integrates with 15 different external systems

Integrations Framework:• API In supported by Web API (CE) and RabbitMQ (EE)• API Out supported by RabbitMQ (EE)

Page 4: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 4

Magento Chat Bot for Facebook Messenger

Page 5: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 5

Use-Case: Store With Hand-Picked Books

• Niche customers with unusual requests

Page 6: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 6

Use-Case: Store With Hand-Picked Books

• Facebook is the best way to reach larger and targeted audience• Facebook Page representing the store• Customers don’t want to leave Facebook to shop in store• Customers ask questions in Messenger

Page 7: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 7

Facebook Chat Bots API

• Manager cannot handle all communications manually

• Facebook webhooks for messages received in the chat

• Facebook API for responses• Backed by simple AI to understand the

context of discussion

Page 8: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 8

Facebook POSTs message to the Callback

Callback.php200 OK

Page 9: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 9

Callback POSTs response to Facebook API

Callback.php API

Page 10: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 10

Integration with Magento

IN/OUT Using Web API

Page 11: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 11

Callback makes API request to Magento

Messenger Callback.php

API

Web API Request

Magento POSTs response to Facebook

Page 12: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 12

Web API Service Interface

Page 13: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 13

Magento API Interfaces

• https://github.com/vrann/magebot

Page 14: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 14

Map Service to the Web API endpoint

Page 15: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 15

What can go wrong?

When customer writes in Messenger:• Blocking request from

callback to Magento• Scalability: Magento can be

overloaded processing other requests

• Availability: Magento API is unreachable at that moment

Messages from customer are lost!

Page 16: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 16

API IN Using RabbitMQ

Page 17: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 17

• Scripts: https://github.com/vrann/http-rabbitmq-writer

Callback puts messages to the Queue

Messenger

API

Callback.php

Magento POSTs response to Facebook

Rabbit MQ

Page 18: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 18

Magento: transform Web API to RabbitMQ

Map message handler to topic in communication.xml

Map consumer to queue in queue_consumer.xml

Run consumer(s):

Page 19: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 19

With Communication & Queue configs

We achieved: Non-Blocking request, response to Facebook sent immediately Scalability handled by Queue Availability handled by QueueCommunication vs Queue:• Communication Framework is an abstraction around

publisher/subscriber. It can be implemented:– with database and HTTP requests instead (webhooks)– in-memory (observers)

• Queue is a Transport layer for Communication– configuration of RabbitMQ adapter and topology

Page 20: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 20

OUT Using RabbitMQ

Page 21: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 21

• Scripts: https://github.com/vrann/http-rabbitmq-writer

Goal: Magento sends messages to Rabbit

Messenger

API

callback.php

response.php

Page 22: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 22

Before: sender interface with the HTTP request

Page 23: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 23

Now: Sender interface with RabbitMQ

Replace preference for MessageSenderInterface in di.xml, Add “Remote” suffix

Interface implementation will be auto-generated:

Page 24: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 24

Publisher & Topology

queue_publisher.xml maps generated topic to the exchange

queue_topology.xml configures exchange to queue bindings

Available from Magento 2.2, before it was in queue.xml

Page 25: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 25

Page 26: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 26

Input Messages Handler

Input Classifier

Search catalog by author

Title, image url, description

For Input Classifier it is better to use

Page 27: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 27

Wrap Up

• We covered:– Reasons behind Magento Web API data structures– Reasons to use queues– Switch from Web API to the RabbitMQ – Code generation of API OUT for RabbitMQ– Communication framework vs Queue framework– Separation of publishers, consumers, topology in 2.2– Input Message Handling– Facebook bots!

• https://github.com/vrann/facebook-chatbot• https://github.com/vrann/http-rabbitmq-writer• https://github.com/vrann/magebot

Page 28: Mage Titans USA 2016 - Eugene Tulika -  Integrations with Magento, end to end story: RabbitMQ, APIs

© 2016 Magento, Inc. Page | 28

Q&A

Write me @vrann (twitter, github)