flutter_xmpp 0.0.32 copy "flutter_xmpp: ^0.0.32" to clipboard
flutter_xmpp: ^0.0.32 copied to clipboard

Flutter XMPP for Realtime Communication, this plugin use Smack Library on Android to connect with XMPP server,currently this plugin still development and will be update ...

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'package:flutter_xmpp/flutter_xmpp.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String rerceiveMessageFrom = '';
  String rerceiveMessageBody = '';

  FlutterXmpp flutterXmpp;

  @override
  void initState() {
    super.initState();
    initXmpp();
  }


  @override
  void dispose() async{
    await flutterXmpp.stop();
    super.dispose();
  }

  Future<void> initXmpp() async{
    var auth = {
      "user_jid": "22637@0.0.0.0/Android",
      "password":"22637",
      "host":"0.0.0.0",
      "port":5222
    };
    flutterXmpp = new FlutterXmpp(auth);

    // login
    await flutterXmpp.login();

    // start listening receive message
    await flutterXmpp.start(_onReceiveMessage,_onError);

    sleep(const Duration(seconds:2)); // just sample wait for get current state

    print(await flutterXmpp.currentState()); // get current state

    // sending Message
    await flutterXmpp.sendMessage("1@0.0.0.0","test","random_id_for_sync_with_sqlite");


    // read Message
    await flutterXmpp.readMessage("1@0.0.0.0","random_id_for_sync_with_sqlite");


    // life cycle, if app not active, kill stream get incoming message ..
    lifeCycle();

    // logout
    await flutterXmpp.logout();

  }

  void lifeCycle() async{
    SystemChannels.lifecycle.setMessageHandler((msg) async{
      if(msg == "AppLifecycleState.inactive" || msg == "AppLifecycleState.suspending" ){
        await flutterXmpp.stop();
      }else if(msg == "AppLifecycleState.resumed"){
        await flutterXmpp.start(_onReceiveMessage, _onError);
      }
      print('SystemChannels> $msg');
      return "Lifecycle";
    });
  }

  void _onReceiveMessage(dynamic event) {
    print(event);
    if(event["type"] == "incoming") {
      setState(() {
        rerceiveMessageFrom = event['from'];
        rerceiveMessageBody = event['body'];
        rerceiveMessageBody = event['id']; // chat ID
      });
    } else {
      setState(() {
        rerceiveMessageFrom = event['to'];
        rerceiveMessageBody = event['body'];
        rerceiveMessageBody = event['id']; // chat ID
      });
    }
  }

  void _onError(Object error) {
    print(error);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('FlutterXMPP'),
        ),
        body: Center(
          child: Text('Incoming or Outgoinng Message: \n$rerceiveMessageFrom\n$rerceiveMessageBody'),
        ),
      ),
    );
  }
}
9
likes
30
pub points
35%
popularity

Publisher

unverified uploader

Flutter XMPP for Realtime Communication, this plugin use Smack Library on Android to connect with XMPP server,currently this plugin still development and will be update ...

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_xmpp