shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

How to use this lib Android + iOS + Web? #255

Closed ganeshshirole closed 3 years ago

ganeshshirole commented 3 years ago

Getting this error

../../.pub-cache/hosted/pub.dartlang.org/mqtt_client-8.2.0/lib/mqtt_browser_client.dart:11:8: Error: Not found: 'dart:html' import 'dart:html'; ^

shamblett commented 3 years ago

Your using the browser client, this only works in the browser, if you are using the VM or flutter you need mqtt_server_client.

ganeshshirole commented 3 years ago

Your using the browser client, this only works in the browser, if you are using the VM or flutter you need mqtt_server_client.

I am using both mqtt_browser_client and mqtt_client and also added platform conditions still getting this error

../../.pub-cache/hosted/pub.dartlang.org/mqtt_client-8.2.0/lib/mqtt_browser_client.dart:11:8: Error: Not found: 'dart:html' import 'dart:html';

shamblett commented 3 years ago

You should be using just the server client, your imports should be -

import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';

Don't import the browser client.

ganeshshirole commented 3 years ago

You should be using just the server client, your imports should be -

import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';

Don't import the browser client.

How can I write conditional import like if I want to import browser client for web and mqtt_server_client for mobile?

ganeshshirole commented 3 years ago

You should be using just the server client, your imports should be -

import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';

Don't import the browser client.

How can I write conditional import like if I want to import browser client for web and mqtt_server_client for mobile?

But why you are not manage platforms in library only so I will use common client for web and mobile both

shamblett commented 3 years ago

I haven't got round to doing it, when this package was written I'm not sure conditional imports existed, if you want this urgently please submit a pull request. Conditional imports are described here in the Dart docs.

alamsz commented 3 years ago

You should be using just the server client, your imports should be -

import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';

Don't import the browser client.

How can I write conditional import like if I want to import browser client for web and mqtt_server_client for mobile?

I do have this problem since I want to use the lib both in app and web, my solution to solve the problem was

import 'app.dart' if (dart.library.html) 'web.dart' as mqttsetup;

...

var client = mqttsetup.setup(serverAddress, uniqueID, port);

where app.dart will contain such as the following

MqttClient setup(String serverAddress, String uniqueID, int port) {
  return MqttServerClient.withPort(serverAddress, uniqueID, port);
}

and web.dart

MqttClient setup(String serverAddress, String uniqueID, int port) {
  return MqttBrowserClient.withPort(serverAddress, uniqueID, port);
}
shamblett commented 3 years ago

Ok thanks for this, I'll put it in the examples directory, may be of use to other users.

ganeshshirole commented 3 years ago

Thank you for your response

On Sat, 20 Mar, 2021, 2:07 pm Steve Hamblett, @.***> wrote:

Ok thanks for this, I'll put it in the examples directory, may be of use to other users.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/shamblett/mqtt_client/issues/255#issuecomment-803272701, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACLG3M2D6NI6ROM6YGFP6A3TERND5ANCNFSM4YAVNW5A .

shamblett commented 3 years ago

Complete, package re published at version 9.1.0.

ganeshshirole commented 3 years ago

Thank you so much

On Thu, 8 Apr, 2021, 3:54 pm Steve Hamblett, @.***> wrote:

Closed #255 https://github.com/shamblett/mqtt_client/issues/255.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/shamblett/mqtt_client/issues/255#event-4567787669, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACLG3M4N2GOP6RR6VRFGFLLTHV77TANCNFSM4YAVNW5A .

adojang commented 1 year ago

A few years late to the party, but can confirm this works properly. Would love to see some kind of autoselect/autoimport feature built into the library if it is still being maintained.