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

Can not listen pusher server #309

Open
bensonarafat opened this issue Dec 26, 2023 · 2 comments
Open

Can not listen pusher server #309

bensonarafat opened this issue Dec 26, 2023 · 2 comments

Comments

@bensonarafat
Copy link

I could connect successfully with pusher at the backend but it wont receive anything.

@MannaYang
Copy link

@bensonarafat
Have you use channel.stream.listen() it?

In my project:

ChatGPT-Flutter-Web

import 'package:web_socket_channel/web_socket_channel.dart';


///
/// Connect Send Socket
///
@Riverpod(keepAlive: true)
WebSocketChannel send(SendRef ref) {
  const backendWebSocketUrl =
      String.fromEnvironment('BACKEND_WS_URL', defaultValue: ApiProvider.wsUrl);
  return WebSocketChannel.connect(
    Uri.parse(
        "${backendWebSocketUrl}chat/receive?token=${ref.watch(tokenProvider)}"),
  );
}

  ///
  /// Listen Stream 
  ///
_userMessageStreamListen() {
    ref.read(sendProvider).stream.listen(
      (jsonData) {
        var data = json.decode(jsonData);
        var result = BaseResult<List<ChatMessageInfo>>.fromJson(
            data,
            (jsonT) => (jsonT as List<dynamic>)
                .map((item) => ChatMessageInfo.fromJson(item))
                .toList());
        result.data != null && result.data!.length == 1
            ? ref.read(chatMessagesProvider.notifier).insertMessageList(result)
            : ref.read(chatMessagesProvider.notifier).selectMessageList(result);
      },
      onDone: () {
        retryUserSocket();
      },
      onError: (error) {
        Logger().d('WebSocket error: $error');
      },
    );
  }

/// send message
ref.read(sendProvider).sink.add(_chatMessageData(next.$1, next.$3));

Because channel.stream is asnyc stream, you should listen it to receive server message, do not use await for(xxxxxxx)

@bensonarafat
Copy link
Author

Okay, great.
I'll try it

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

2 participants