Slide 1

Slide 1 text

Real-time apps with GraphQL, Node.js and MQTT Jacopo Daeli Software Engineer San Francisco, Feb 2nd 2017 SFNode

Slide 2

Slide 2 text

What a real-time applications are? They are apps that must guarantee response within specified time constraints to specific events. These time constraints depend from the kind of application you are developing. Self-driving cars are one of the most amazing example of real-time applications. The have really strict time constraints… otherwise you will be dead.

Slide 3

Slide 3 text

What a real-time applications are? Chat messaging applications should be real-time too. I believe in a good messaging application a message should go from UserA to UserB within 100 ms. Facebook built a really cool scalable mobile-first infrastructure for Messenger capable to send a message from one user to another in under 100 ms. FB Messenger is clearly a good example of real-time chat messaging application.

Slide 4

Slide 4 text

Protocols for getting data down? Pull-based approach. The client first receive a lightweight message (maybe over web sockets) indicating new data is available, and then it sends the server a more complicated http request asking for the data. Push-based approach. The client first retrieves an initial snapshot of the data (generally with an http request) and then subscribes to delta updates, which are immediately pushed to the app (maybe through MQTT).

Slide 5

Slide 5 text

What is GraphQL? GraphQL is a query language for your API, and a server- side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.

Slide 6

Slide 6 text

Why GraphQL? Mainly because: • You may have different clients (e.g. web, iOS, Android) that render data differently • You may have to care about latency and bandwidth, especially in real-time applications • You may want to quickly decouple front-ends and back-ends GraphQL allows each client to query exactly what it needs, in the format it needs. It is the clean layer of abstraction between servers and clients that an ad-hoc REST approach will never be able to provide.

Slide 7

Slide 7 text

GraphQL in practice GraphQL exposes three operations: Queries (to read data), Mutations (to write data), and Subscriptions (to receive data updates).

Slide 8

Slide 8 text

What is MQTT? MQTT (MQ Telemetry Transport) is a pubsub-based lightweight messaging protocol to use on top of the TCP/ IP protocol.

Slide 9

Slide 9 text

Why MQTT? MQTT is ideal for real-time applications (especially if mobile) because of its small size, low power usage, minimised data packets, and efficient distribution of information to one or many receivers.

Slide 10

Slide 10 text

The concept GraphQL MQTT API MQTT

Slide 11

Slide 11 text

The concept GraphQL MQTT Subscribe Subscribe API MQTT _: Client(query x)

Slide 12

Slide 12 text

The concept GraphQL MQTT Subscribe Subscribe Mutation API _: Client(query x)

Slide 13

Slide 13 text

The concept GraphQL MQTT Subscribe Subscribe Mutation API Run Mutation Mutation Response MQTT _: Client(query x)

Slide 14

Slide 14 text

The concept GraphQL MQTT Subscribe Subscribe Mutation API Run Mutation Mutation Response Publish MQTT _: Client(query x)

Slide 15

Slide 15 text

The concept GraphQL MQTT Subscribe Subscribe Mutation API Run Mutation Mutation Response Publish MQTT MQTT _: Client(query x)

Slide 16

Slide 16 text

The concept GraphQL MQTT Subscribe Subscribe Run Subscriptions Mutation API Run Mutation Mutation Response Publish

Slide 17

Slide 17 text

The concept GraphQL MQTT Subscribe Subscribe Run Subscriptions GraphQL Responses Mutation API Run Mutation Mutation Response Publish

Slide 18

Slide 18 text

The concept GraphQL MQTT Subscribe Push update Subscribe Push update Run Subscriptions GraphQL Responses Mutation API Run Mutation Mutation Response Publish

Slide 19

Slide 19 text

Why Node.js? Node is fun. Node has a well-maintained GraphQL package. It also has two fantastic modules call mqtt- connection and mqtt. All of them available on npm. It is easier to use these three libraries together to build a fully working Node.js application.

Slide 20

Slide 20 text

Demo Code available on GitHub: https://github.com/JacopoDaeli/realtime-graphql

Slide 21

Slide 21 text

Questions? @JacopoDaeli [email protected]