Share
Embed
SFNode - Real-time apps with GraphQL, Node.js and MQTT
Presentation slides for SFNode, hosted by PostMates in San Francisco, Feb 2nd 2017.
Jacopo Daeli
February 02, 2017
Transcript
21 slides
-
Real-time apps with GraphQL, Node.js and MQTT Jacopo Daeli Software
Engineer San Francisco, Feb 2nd 2017 SFNode -
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. -
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. -
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). -
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. -
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. -
GraphQL in practice GraphQL exposes three operations: Queries (to read
data), Mutations (to write data), and Subscriptions (to receive data updates). -
What is MQTT? MQTT (MQ Telemetry Transport) is a pubsub-based
lightweight messaging protocol to use on top of the TCP/ IP protocol. -
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. -
The concept GraphQL MQTT Subscribe Subscribe Mutation API Run Mutation
Mutation Response MQTT <resource>_<id>: Client<id>(query x) -
The concept GraphQL MQTT Subscribe Subscribe Mutation API Run Mutation
Mutation Response Publish MQTT <resource>_<id>: Client<id>(query x) -
The concept GraphQL MQTT Subscribe Subscribe Mutation API Run Mutation
Mutation Response Publish MQTT MQTT <resource>_<id>: Client<id>(query x) -
The concept GraphQL MQTT Subscribe Subscribe Run Subscriptions Mutation API
Run Mutation Mutation Response Publish -
The concept GraphQL MQTT Subscribe Subscribe Run Subscriptions GraphQL Responses
Mutation API Run Mutation Mutation Response Publish -
The concept GraphQL MQTT Subscribe Push update Subscribe Push update
Run Subscriptions GraphQL Responses Mutation API Run Mutation Mutation Response Publish -
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.