Can not listen pusher server
bensonarafat opened this issue · 2 comments
bensonarafat commented
I could connect successfully with pusher at the backend but it wont receive anything.
MannaYang commented
@bensonarafat
Have you use channel.stream.listen() it?
In my project:
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 commented
Okay, great.
I'll try it