Loading...
Home
  • News
    • Android News
    • Droidcon News
  • Conferences
    • Conference Videos
    • Upcoming Conferences
    • Become a Partner
    • Past Events
  • Community
    • droidcon Team
    • Forums
    • Keep Me Informed
    • How to Hold a Droidcon
  • Jobs
    • jobs.droidcon.com
Sign In

Global CSS

 

GraphQL WebSocket subscriptions on Android using Apollo

 

 
Josias Sena
Android Engineer — https://www.josiassena.com
Published: November 13, 2019
Tweet
Share
 

Recently my team and I worked on implementing WebSockets (known as subscriptions in GraphQL), to allow users to talk to each other in real-time. During this time, we saw that the documentation to this using the Apollo client for Android was pretty bad! So in this post, I will be talking about our findings and how this can be done, with the hopes that it might be helpful for others as well who might be having the same problem.

This post will not be about how to get started with Androids Graphql library Apollo, or what GraphQl is.

GraphQL subscriptions are a way to push data from the server to the clients that choose to listen to real time messages from the server. Subscriptions are similar to queries in that they specify a set of fields to be delivered to the client, but instead of immediately returning a single answer, a result is sent every time a particular event happens on the server.

A common use case for subscriptions is notifying the client side about particular events, for example the creation of a new object, updated fields and so on.

https://www.apollographql.com/docs/react/data/subscriptions/


Note: Everything that you will see here will be written for simplicity and understanding. There will be no Dagger or any special architecture or library usage, to make it easy for anyone to introduce into their codebase.

 

Let’s get started!


Defining a subscription

GraphQL subscriptions are pretty simple to define, just like we do with our queries and mutations, we define subscriptions in our .graphql files using the subscription keyword.


 

Enabling Apollo WebSocket subscriptions

Once we have created our subscription, let’s enable them on our apollo client.


 

Code Explained

  • HeadersProvider contains shared headers to be used across multiple apollo clients.
     
  • sharedOkHttpClientBuilder The same OkHttp builder used for creating the original Apollo client, even though the Request headers are not shared, other configurations are shared.
     
  • okHttpClient the new OkHttp client created for subscriptions, with an optional interval.
     
  • subscriptionTransportFactory The most important piece here, this is what we use to enable subscriptions.
     

pingInterval(KEEP_ALIVE_INTERVAL, TimeUnit.SECONDS) is optional here, it is sometimes required to ping the endpoint every so often, to make sure the connection stays open.
 

WebSocketSubscriptionTransport.Factory takes the subscription URL which usually starts with and the OkHttpClient to use.
 

That is it, this is all it takes to enable subscriptions.
 

Subscribing at last!

This is the moment we have all been waiting for, actually subscribing and receiving data!

First, we create a simple generic function type that supports subscribing to any kind of GraphQL subscription (feel free to use your own).


 

All this function does is allow us to create a subscription of any type, the internals should be very similar to what you might be doing for queries and mutations, except this time we call apolloClient.subscribe instead.


 

Conclusion

That is all! Enabling and subscribing to WebSocket subscriptions in GraphQL is actually pretty simple once you see it once, and they are a lot of fun to work with. I hope you enjoyed the article and have as much fun as we are having with them here at Dubsmash, until next time!
 



Originally published at https://josiassena.com


 

Tags: GraphQL, Android, Android App Development, Mobile App Development, AndroidDev


 

View original article at: 


 

 

Android News
Creating an Expandable Floating Action Button in Android — Part 2
Creating an Expandable Floating Action Button in Android — Part 2

By TJ

It’s been a little over a year since the post above, and a lot of things can change in a year. Specifically, I’ve since moved to Kotlin as my primary language for Android development, and I’ve found a cleaner and more aesthetically pleasing way to create an expandable floating action button in Android; let’s talk more about the latter.

 

By ProAndroidDev -
Android News
Phantom Types in Kotlin
Phantom Types in Kotlin

By Danny Preussler

While working on my KotlinConf presentation about types I stumbled upon this post by Maximiliano Felice about Phantom types with Scala. The idea: prevent objects from having an illegal state or forbid illegal operation at compile time. The example given was a class Door. A door is either open or closed. In Kotlin this would look like:

By ProAndroidDev -
Android News
Exploring the new CameraX library in Android
Exploring the new CameraX library in Android

By Siva Ganesh Kantamani

CameraX is a Jetpack support library which is in Alpha at present. CameraX library is build to simplify the usage of camera features in app development. CameraX provides an in-depth analysis of what camera is pointed at through Image Analysis and also provides a way to use built-in device camera features like HDR, portrait mode and so on through extensions.

By ProAndroidDev -
Android News
A fusion between WorkManager and AlarmManager
A fusion between WorkManager and AlarmManager

By Stavro Xhardha

Android’s WorkManager has been around for a while. However my own expectations about it were a little higher. I wished that the WorkManager could fire events at exact timing. But since it was made to respect doze mode, I should respect that too. That means that if the phone is idle, the WorkManager  won’t run.

By ProAndroidDev -

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2019. All rights reserved.

powered by Breakpoint One