Skip to content
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

Realtime socket communication is not normal. #24

Closed
honeyhead opened this issue Mar 1, 2023 · 9 comments
Closed

Realtime socket communication is not normal. #24

honeyhead opened this issue Mar 1, 2023 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@honeyhead
Copy link

honeyhead commented Mar 1, 2023

I brought a new error.

await table.On(ChannelEventType.Insert, (sen, args) =>
{
Userworklist.Add(args.Response.Model<WORKS>());
});

I am checking to define a table called WORKS as Realtime as below and import it.
And when I uploaded the file, I could see this message in the debug.

[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 100%
[TrafficStats] tagSocket(115) with statsTag=0xffffffff, statsUid=-1

The problem is that you are receiving Realtime, but you cannot get anything.
And if you repeat the same behavior, it comes out as below.

[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 0%
[0:] 100%

[TrafficStats] disappears and real-time events are received.

When you receive a Realtime event, the first one feels ignored.

Do you happen to know about the content? I'm a MAUI developer, but I don't think it's an Android problem because I did the same test in the Windows environment.

@honeyhead honeyhead added the bug Something isn't working label Mar 1, 2023
@honeyhead
Copy link
Author

honeyhead commented Mar 1, 2023

image

I checked it out.
I created the channel with authenticated SupabaseClient.Auth.CurrentSession and connected it to the socket through the Subscribe() method.

After checking, the item in Realtime's Subscription table was anon permission.

To change this, I gave the new client an AccessKey, but the Subscribe method did not work this time. (i.e. not registered in the subscription table)

Judging from this, I thought it only works for the anon key as described in the description in the supabase, but as I told you in the last article, it works from the second time.

image

When I uploaded the file, the anon permission of the linked account in the Subscribe table was changed to authenticated.

In fact, I have a question about this.

I chose the table as From in StatelessClient and declared it because it has On, but I couldn't test this because there was an error because I don't have Connect or Subscribe.

And for some reason, I checked that the upload progress was recorded in Debug in this update. What went straight to 0% ->100% was fixed. Thank you!

@honeyhead
Copy link
Author

honeyhead commented Mar 1, 2023

Hi. After a lot of tests, I checked what the problem is.

await SupabaseClient.Realtime.ConnectAsync();
await SupabaseClient.From<WORKS>().On(ChannelEventType.All, (sen, ea) =>
{

});

Since I wrote it like this, I checked that when I called the Subscribe() method, a row was added to the realtime->subscription table and claims_role became anon.

After 10 seconds, I experienced that the claims_role changes to authenticated when I refresh.

The reason is that On contains a code that adds user_token as a parameter.

First of all, there's a problem that I don't know why there's a 10-second delay, and if I can update this quickly, I think I can solve the fundamental problem.

The problem is that I didn't use On, I used SupabaseClient.Realtime.Channel("realtime", "public", "WORKS").

WORKS is my table here.

If I make a channel with this, it's fixed as anon and it didn't change. And when I looked at the code, I found something that I had to send parameters when I made the channel.

It's a shame there wasn't such an explanation. I don't think anyone knows that we have to send user_token to the parameter.

As I did so, I confirmed that anon changed to authenticated after about 10 seconds like the On written above.

But I can't receive any changes in real time because I'm anon for 10 seconds anyway. If I could check the changes in climes_role as an event, I would block other movements or turn the busyIndicator, but I don't know at the moment.

If there is a user Access Token key in "Authorization" by looking up the header inside the client when creating the channel, I think the code will generate user_token.

Of course, depending on the developer's intention, some people want to do it with anon.

And I told you that I uploaded the file and the permission changed?

At this time, there seems to be something updating claims_role on the server, so if call taht, wouldn't we be able to reduce the delay of 10 seconds?

I'm sorry I only found weird things :)

@honeyhead
Copy link
Author

honeyhead commented Mar 1, 2023

Oh, I think I found it.

After running the Subscribe() method to subscribe to events coming from the Channel, the developer must issue commands or write code.

1)
SupabaseClient.Auth.SetAuth(SupabaseClient.Auth.CurrentSession.AccessToken);

or

2)
channel.Push("access_token", payload: new Dictionary<string, string>
{
{ { "access_token", SupabaseClient.Auth.CurrentSession.AccessToken }
});

Actually, the contents of No. 2 are included in SetAuth.
The developer only needs to operate code 1, but I think it can be customized in some cases.

When I wrote it like this, I confirmed that the claims_role was changed from anon to authenticated within a term of 2 seconds.

It would be more convenient to automatically check SetAuth() when the Subscribe() method works, but I'm proud to find the answer anyway.

Thank you for making a good service!
I hope it helps someone.

@Schroedi
Copy link

Schroedi commented Mar 1, 2023

I experienced the same. Calling SetAuth is not enough. Pushing the access token did the trick for me, even without the SetAuth call. It did not matter whether I had AutoConnectRealtime on or off.
Thanks for your digging @honeyhead !

I still think this is a bug or at least needs better documentation.

@acupofjose acupofjose reopened this Mar 1, 2023
@acupofjose
Copy link
Collaborator

@honeyhead thanks so much for digging into this. I was trying to debug last night and was running to the same issue. I think the current client has fallen behind on the proper handshake from the current deployed supabase realtime version! So this is very helpful. The intended implementation would be that the developer shouldn't have to call SetAuth or push an access token, it should be handled automatically by the library.

I'll look into getting your suggestions pushed into the next version (with credit of course)!

@acupofjose acupofjose self-assigned this Mar 1, 2023
@acupofjose
Copy link
Collaborator

v5.0.2 will automatically push the access token to any channel on subscription - the timing issue you mentioned should also be fixed. Your name is included in the changelog. Thanks for your help @honeyhead!

@honeyhead
Copy link
Author

honeyhead commented Mar 5, 2023

@acupofjose
Thanks! I checked that Claim changes to Authenticated without SetAuth() or channel.Push("access_token") after subscribe().

However, it seems inevitable that there will be a two-second delay. If so, is there a way to event check if Claim_Role is changed?

@acupofjose
Copy link
Collaborator

I suppose we could make it to where the async Subscribe() only returns after an acknowledgement from the server? Which should be after the claims role has been given. Thoughts?

@honeyhead
Copy link
Author

honeyhead commented Mar 7, 2023

@acupofjose

I think each event should be defined before the Subscribe() method.

However, in the process of initializing the supabase client, autorealtime may be given as true.

What is certain is that the client must be able to recognize the state of the claim_role being changed before or after Subscribe() so that developers can deal with issues that they cannot receive due to lack of permission.

If Supabase does not create a separate event, make sure that Claim_role changes and the code will work after await client.Subscribe()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants