简体   繁体   English

使用ably flutter package,如何订阅和收听Laravel和Flutter的活动?

[英]Using the ably flutter package, how can I subscribe to and listen to an event in Laravel and Flutter?

I'm developing a chat application using Laravel and flutter. I'm using ably_flutter package to make it realtime.我正在使用 Laravel 和 flutter 开发聊天应用程序。我正在使用 ably_flutter package 使其实时。 The channel is created successfully but it's faild to listen an event or a message.通道创建成功,但监听事件或消息失败。

final clientOptions = ably.ClientOptions( 
      key: 'rVPjew.ydfBPA:JqRRY9JI49_L9l8CsfvMxXuxhMeyQzgEo6apWE');

 subscribeAbly() async {
    ably.Realtime realtime = ably.Realtime(options: clientOptions);

    realtime.connection
        .on(ably.ConnectionEvent.connected)
        .listen((ably.ConnectionStateChange stateChange) async {
print(stateChange.current)// connected
      ably.RealtimeChannel channel = realtime.channels.get('public.room');
      channel.subscribe(name: 'message.new').listen((ably.Message message) {
        print("message is fired");//it's not working
        final data = jsonEncode(message.data);
        final response = jsonDecode(data)['message'] as Map<String, dynamic>;
        setState(() {
          mapData.insert(0, {"message": "From ably", "is_sender": false});
        });
      });
    });

   @override
   initState(){
   super.initState();
    subscribeAbly()
 }      

I'm using Flutter 2.8 and ably_flutter 1.2.15 Any assistance would be much appreciated.我正在使用 Flutter 2.8 和 ably_flutter 1.2.15 任何帮助将不胜感激。

As a debugging step, if you publish a message from within flutter to this channel using channel.publish() do you receive an event?作为调试步骤,如果您使用channel.publish()从 flutter 向该频道发布消息,您会收到事件吗?

If not then something might be wrong with your setup in Flutter. One thing I'd try would be to move the realtime object declaration outside of the async function scope and make it a property instead.如果不是,那么您在 Flutter 中的设置可能有问题。我尝试的一件事是将realtime object 声明移到异步 function scope 之外,并将其设为属性。

Other than that - again as a debugging step try to subscribe to all events on the channel:除此之外 - 再次作为调试步骤尝试订阅频道上的所有事件:

channel.subscribe()

instead of:代替:

channel.subscribe(name: 'message.new')

and double check if the channel name is correct and you've set up Laravel to publish messages to the same channel:并仔细检查频道名称是否正确,并且您已设置 Laravel 以将消息发布到同一频道:

realtime.channels.get('public.room')

Could you also please elaborate on how you are publishing messages on the laravel side?能否请您详细说明您是如何在 laravel 端发布消息的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM