Skip to content

Subscription Failing Without Error #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bstillitano opened this issue Apr 11, 2019 · 17 comments
Closed

Subscription Failing Without Error #103

bstillitano opened this issue Apr 11, 2019 · 17 comments

Comments

@bstillitano
Copy link

I am subscribing to a query on a table that has liveQuery enabled however the subscription seems to just be failing completely. The code is below:

fun startListeningToNotifications() {
        //Build Live Query Client
        val parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient()

        //Build Query
        var parseQuery = ParseQuery.getQuery<ParseObject>("Notifications")
        parseQuery.whereEqualTo("toUser", ParseUser.getCurrentUser())
        parseQuery.orderByAscending("createdAt")
        parseQuery.findInBackground { objects, e ->
            //Do something with notifications
        }

        //Build Live Query Listener
        var subscriptionHandling: SubscriptionHandling<ParseObject> = parseLiveQueryClient.subscribe(parseQuery)
        subscriptionHandling.handleSubscribe {
            Toast.makeText(this, "SUBSCRIBED", Toast.LENGTH_LONG).show()
        }
        subscriptionHandling.handleEvents { query, event, `object` ->
            fetchNotificationCount()
        }

        subscriptionHandling.handleError { query, exception ->
            Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
        }
    }

Any help with this is highly appreciated. I have looked at @rommansabbir project and it does nothing that the example project doesn't show us. Really stuck on this one.

@bstillitano
Copy link
Author

Sample Activity here:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

        initParse(this)
    }

    fun initParse(context: Context) {
        //Setup Parse
        Parse.initialize(
                Parse.Configuration.Builder(context)
                        .applicationId("applicaitonId")
                        .clientKey("clientKey")
                        .server("serverURL")
                        .enableLocalDataStore()
                        .build()
        )

        //Setup Config
        ParseConfig.getInBackground()

        //Sample LiveQuery
        startListeningToNotifications()
    }

    fun startListeningToNotifications() {
        //Build Live Query Client
        val parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient()

        //Build Query
        var parseQuery = ParseQuery.getQuery<ParseObject>("Notifications")
        parseQuery.whereEqualTo("toUser", ParseUser.getCurrentUser())
        parseQuery.orderByAscending("createdAt")
        parseQuery.findInBackground { objects, e ->
            //Do something with notifications
        }

        //Build Live Query Listener
        var subscriptionHandling: SubscriptionHandling<ParseObject> = parseLiveQueryClient.subscribe(parseQuery)
        subscriptionHandling.handleSubscribe {
            Toast.makeText(this, "SUBSCRIBED", Toast.LENGTH_LONG).show()
        }
        subscriptionHandling.handleEvents { query, event, `object` ->
            Toast.makeText(this, "EVENT RECEIVED", Toast.LENGTH_LONG).show()
        }
        subscriptionHandling.handleError { query, exception ->
            Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}

@bstillitano
Copy link
Author

Anything on this?

@rommansabbir
Copy link
Member

rommansabbir commented Apr 23, 2019

This is what i did.

Important
Make Sure, your parse server version grater than 2.8.4.
Enable live query from Back4App by activating "Activate Back4App Hosting", create a subdomain, select all table that you want to live query.

Important
parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient(URI("wss://" + context!!.getString(R.string.live_query_app_name) + ".back4app.io/"))
live_query_app_name is your subdomain
Handle single event instead of multiple

subscriptionHandling = parseLiveQueryClient!!.subscribe(requestStatusLiveQuery)
subscriptionHandling!!.handleEvent(SubscriptionHandling.Event.CREATE) { query, `object` -> }
subscriptionHandling!!.handleEvent(SubscriptionHandling.Event.UPDATE) { query, `object` ->}
subscriptionHandling!!.handleEvent(SubscriptionHandling.Event.DELETE) { query, `object` -> }

Still working good for me..

You can try this:
https://github.com/rommansabbir/ParseLiveQuery-Android/

@mmimeault
Copy link
Contributor

Hello @bstillitano thank you for trying live query

however the subscription seems to just be failing completely
Can you details on this.
Do you see any relevant logs on the client
Or on the server?

Thanks

@bstillitano
Copy link
Author

bstillitano commented Apr 23, 2019

@rommansabbir please stop. As many people have said, your solution only works for back4app and not the common setup of self hosted parse instances.

@mmimeault thanks for taking the time to look at this. I truly do appreciate it. I am not seeing any client side errors or server side errors or logs.

The strange thing is though that this works fine when run in a Java project which leads me to believe that there may be something wrong with the Kotlin implementation/migration of this library.

When I say

however the subscription seems to just be failing completely

I mean that none of the subscription events are being fired including errors, subscriptions and object updates

Any help is appreciated.

@rommansabbir
Copy link
Member

rommansabbir commented Apr 23, 2019 via email

@bstillitano
Copy link
Author

@mmimeault are you able to assist with this?

@bstillitano
Copy link
Author

bstillitano commented Apr 30, 2019

Wondering if this is of any help to you @mmimeault. Really need to get this issue resolved as over on StackOverflow, multiple people are having the same issue.

image

@mmimeault
Copy link
Contributor

Hello, It is really to debug that kind of issue remotely.
I have been using this live query client since the beginning and it is still working perfectly.

If you put some breakpoint into the OkHttp3WebSocketClient, do you any of the callback methods called? Maybe there is an error not thrown back to the live query client.

There is 5 callback to the okhttp3 websocket client (WebSocketListener). onOpen, onMessage(2), onClosed and onFailure. Maybe putting some breakpoints there will help us debug the issue. Any of those method are called when starting the subscription?

Thanks

@bstillitano
Copy link
Author

@mmimeault you're right. I'm getting the following stacktrace when subscribing to any queries:
image

Do you have any guidance on where to from here? LiveQuery is definitely running.

@mmimeault
Copy link
Contributor

mmimeault commented May 1, 2019

Just before digging into the error itself, you should add this callback after getting the client from the factory

parseLiveQueryClient.registerListener(new ParseLiveQueryClientCallbacks() {
    @Override
    public void onLiveQueryClientConnected(ParseLiveQueryClient client) {

    }

    @Override
    public void onLiveQueryClientDisconnected(ParseLiveQueryClient client, boolean userInitiated) {

    }

    @Override
    public void onLiveQueryError(ParseLiveQueryClient client, LiveQueryException reason) {

    }

    @Override
    public void onSocketError(ParseLiveQueryClient client, Throwable reason) {

    }
});

You would get the error you see in your debugger in that onSocketError callback.

@mmimeault
Copy link
Contributor

I am not an expert about sockets nor the parse server implementation.
But from what I can understand here, it seem to be an issue with your server.
1- Did you enabled LiveQuery on the parse server?
2- Is the port opened ? Maybe it is blocked by a firewall or routing. Where is your parse server hosted?

Maybe you can try just simply opening a connection to the port in the terminal. You can use telnet to do it. (telnet hostname port) (brew install telnet, if not installed and you are using a mac)

But if it failed at this point with that kind of error, I'm 99.99% sure it is one of the issue I mentioned above.

@mmimeault
Copy link
Contributor

@bstillitano Can you explain why you closed the ticket. Thanks

@bstillitano
Copy link
Author

bstillitano commented May 4, 2019

@mmimeault I closed it because I resolved the issue.

Using your tip of debugging the socket, I found that I was getting a 403. I thought this may have been a setup issue on my end so changed a few things around but this did not help.

After restoring my server to its original setup, I found that by specifying the port directly in the parse server URL in the SDK it connects properly and subscriptions work fine.

Tried with a back4app and heroku setup along with my google cloud setup and all to the same result so this looks like a limit of the SDK or OkHttp

@Jawnnypoo
Copy link
Member

It is not a limit of the SDK or OkHttp to have to specify the port, if you are using a non-standard port, it makes sense that you would have to specify it.

@bstillitano
Copy link
Author

bstillitano commented May 5, 2019 via email

@Jawnnypoo
Copy link
Member

I've set up quite a few Parse server instances on many different providers with the standard setup and have never had to specify the port in the Android SDK. Feel free to open a PR on the docs if you would like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants