hasura_connect
hasura_connect copied to clipboard
Starting two different subscriptions at the same time throws "an operation already exists with this id"
Problem
In my app, I start two different subscriptions with two different queries at the start of the app (i.e., one listens for messages and one listens for notifications). Doing so for some reason throws the following error
{extensions: {path: $, code: start-failed}, message: an operation already exists with this id: "...... the id/key ...... " }
HasuraRequestError: an operation already exists with this id: "....... the id/key (same as above) ......"
A simple code to reproduce:
void main() async {
final url = '.....';
final headers = {......};
final query1 = 'subscription messages { .......... }';
final query2 = 'subscription notifications { ...... }';
final hasuraClient = HasuraConnect(url, headers: headers);
(await hasuraClient.subscription(query1)).listen((event) {});
(await hasuraClient.subscription(query2)).listen((event) {});
}
(In my app, I have them in separate places but they pretty much start at the same time)
a workaround:
adding some delay before one of the subscriptions makes solve the problem but it's not optimal (I found 300ms to be a good delay, anything lower will throw the error).
I'm with the same problem. Add a delay doesn't feel like a good thing.
Maybe it is related to what was told here?
Update
I've tried adding some delay and in my case only works with 1 second which is pretty bad to wait.
In my case the app listens to 20 tables. I had a query that worked perfectly. Then I converted it into a subscription and ran into the fact that a subscription can only query one root field (http://spec.graphql.org/June2018/#sec-Single-root-field). So I rebuilt into 20 separate subscriptions. And ran into this issue:
I/flutter (20244): {extensions: {path: $, code: start-failed}, message: an operation already exists with this id: c3Vic2NyaXB0aW9ucm93c1N1YnNjcmlwdGlvbnJvd3NMYXN0U2VydmVyUmV2QXR0aW1lc3RhbXB0enJvd3N3aGVyZXNlcnZlcnJldmF0Z3Ryb3dzTGFzdFNlcnZlclJldkF0aWR0YWJsZWlkcGFyZW50aWRnZW9tZXRyeWdlb21ldHJ5bmdlb21ldHJ5ZWdlb21ldHJ5c2dlb21ldHJ5d2RhdGFjbGllbnRyZXZhdGNsaWVudHJldmJ5c2VydmVycmV2YXRyZXZwYXJlbnRyZXZyZXZpc2lvbnNkZXB0aGRlbGV0ZWRjb25mbGljdHM=}
Seems like a killer for my app 😢
I can reproduce this error:
- running only one subscription works
- starting two or more subscriptions results in
an operation already exists with this id
I looked at the code. And wondered if the key passed could be the issue (https://github.com/Flutterando/hasura_connect/blob/master/lib/src/presenter/hasura_connect_base.dart#L177).
But passing in own keys does not change the behaviour.
I found the reason for this issue:
When creating a subscription hasura_connect checks if a connection already exists. If not, it creates it before returning the snapshot. But it returns the snapshot without awaiting the connection to be established: https://github.com/Flutterando/hasura_connect/blob/master/lib/src/presenter/hasura_connect_base.dart#L204.
I created a pull request to await the connection to be established: https://github.com/Flutterando/hasura_connect/pull/99
You can use my branch in the meantime (based on v3.0.4):
hasura_connect:
git:
url: https://github.com/barbalex/hasura_connect
ref: master
Im testing in v1.3.2 Hasura, and i cant get the error :C I add the test on playground.dart file in test folder @barbalex can u simulate that?
At least, in version "4.0.0+4" I am still having this problem.