Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

How to connect to Realtime server #10

Closed
SamuraiHub opened this issue Jun 16, 2021 · 8 comments
Closed

How to connect to Realtime server #10

SamuraiHub opened this issue Jun 16, 2021 · 8 comments
Labels
documentation Improvements or additions to documentation

Comments

@SamuraiHub
Copy link

SamuraiHub commented Jun 16, 2021

I am trying to use the following code:

import 'package:realtime_client/realtime_client.dart';

void main() async {
  final socket = RealtimeClient('ws://SUPABASE_API_ENDPOINT/realtime/v1',
      params: {
        'apikey':
            'My API Key'
      },
      // ignore: avoid_print
      logger: (kind, msg, data) => {print('$kind $msg $data')});

  final channel = socket.channel('realtime:public'); //:classTest:chatID.eq.2
  channel.on('DELETE', (payload, {ref}) {
    print('channel delete payload: $payload');
  });
  channel.on('INSERT', (payload, {ref}) {
    print('channel insert payload: $payload');
  });

  socket.onMessage((message) => print('MESSAGE $message'));

  // on connect and subscribe
  socket.connect();
  channel.subscribe().receive('ok', (_) => print('SUBSCRIBED'));
}

But I get the following:

flutter: transport connected to ws://supabase_api_endpoint/realtime/v1/websocket?apikey=My API Key&vsn=1.0.0 null
flutter: push realtime:public ChannelEvents.join () {}
flutter: transport WebSocketChannelException: WebSocketChannelException: SocketException: Failed host lookup: 'supabase_api_endpoint' (OS Error: No such host is known.

, errno = 11001) null
flutter: channel error realtime:public null
flutter: transport close 

What RealtimeClient host can I connect to? how to resolve this problem? thanks

@SamuraiHub SamuraiHub added the documentation Improvements or additions to documentation label Jun 16, 2021
@w3b6x9
Copy link
Member

w3b6x9 commented Jun 16, 2021

@SamuraiHub did you spin up a Supabase project or are you self-hosting?

If you have a Supabase project, the SUPABASE_API_ENDPOINT should look something like abcdabcdabcdabcdabcd.supabase.co where abcdabcdabcdabcdabcd is your Supabase project ref.

@SamuraiHub
Copy link
Author

Thanks for replying. I actually created a Supabase project. is the Supabase project ref found in the settings? I will look for it and if I found it I will replace it and see if it will work or not.

@w3b6x9
Copy link
Member

w3b6x9 commented Jun 16, 2021

@SamuraiHub yes, go to your project settings and it should be under API

@SamuraiHub
Copy link
Author

SamuraiHub commented Jun 16, 2021

Yes it is now working but now, But the channel is not subscribed as I do not see the print statement.

you know a reason for this? Thanks.

@w3b6x9
Copy link
Member

w3b6x9 commented Jun 16, 2021

@SamuraiHub can you try channel.subscribe().receive('error', (reasons) => print(reasons)); and see if any errors are printed?

@SamuraiHub
Copy link
Author

The Socket is connected but the channel.subscribe() does not seem to do anything. I tried what you stated but also see nothing printed. I only see these kind of messages printed:

flutter: push phoenix ChannelEvents.heartbeat (1) {}
flutter: receive error phoenix phx_reply (1) {response: {reason: unmatched topic}, status: error}
flutter: MESSAGE {event: phx_reply, payload: {response: {reason: unmatched topic}, status: error}, ref: 1, topic: phoenix}
flutter: push phoenix ChannelEvents.heartbeat (2) {}
flutter: receive error phoenix phx_reply (2) {response: {reason: unmatched topic}, status: error}
flutter: MESSAGE {event: phx_reply, payload: {response: {reason: unmatched topic}, status: error}, ref: 2, topic: phoenix}

Thanks.

@phamhieu
Copy link
Member

Hi @SamuraiHub, thanks for reporting the bug.
It's fixed already. Can you try again and let me know if you are still facing the issue.

@SamuraiHub
Copy link
Author

Well, I am now getting:

flutter: push phoenix ChannelEvents.heartbeat (1) {}
flutter: receive ok phoenix phx_reply (1) {response: {}, status: ok}
flutter: MESSAGE {event: phx_reply, payload: {response: {}, status: ok}, ref: 1, topic: phoenix}

Which seems ok but it is sill not subscribing. this channel.subscribe().receive('ok', (_) => print('SUBSCRIBED')); does not print Subscribed nor this channel.subscribe().receive('error', (reasons) => print(reasons) prints reasons. I am howerver using something else. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants