Flutter WebSocket works in web application but not in android emulator - flutter

'final channel = WebSocketChannel.connect(
Uri.parse('wss://echo.websocket.org'),
);'
can create and connect in web application
but can't connect in android emulator

There are two libraries implementing WebSocket class - one that works on web, the other on Andoroid.
You can sort this out by creating a simple factory function. You will need to create several files:
websocket_client_factory.dart
export 'websocket_client_factory_null.dart'
if (dart.library.html) 'websocket_client_factory_web.dart'
if (dart.library.io) 'websocket_client_factory_server.dart';
websocket_client_factory_null.dart
import 'package:web_socket_channel/web_socket_channel.dart';
WebSocketChannel makeWsClient(String url) {
throw 'Platform not supported';
}
websocket_client_factory_server.dart
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
WebSocketChannel makeWsClient(String url) =>
IOWebSocketChannel.connect(url, protocols: ['graphql-ws']);
websocket_client_factory_web.dart
import 'package:web_socket_channel/html.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
WebSocketChannel makeWsClient(String url) =>
HtmlWebSocketChannel.connect(url, protocols: ['graphql-ws']);
Now, from your original file just import the factory file, and call the factory function:
import 'package:./websocket_client_factory.dart';
...
var channel = makeWsClient(wssUrl);

In my project WebSocket connection needs authentication
In web application automatically pass cookies and authentication token in
websocket connection
But in Mobile Application we need to set cookies and authentication
token manually
When I done this resolved my issue

Related

firebase/flutter - App Check debug provider in an emulator

I try to get a local debug token according to the instructions, but I don't see any in the console. I implemented this code as given. Is it maybe because the release and debug app-check don't work at the same time?
Guide: https://firebase.google.com/docs/app-check/android/debug-provider?hl=en#java_1, Point 3 doesn't work for me
MainActivity.java
import com.google.firebase.FirebaseApp;
import com.google.firebase.appcheck.FirebaseAppCheck;
import com.google.firebase.appcheck.playintegrity.PlayIntegrityAppCheckProviderFactory;
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory;
FirebaseApp.initializeApp(/*context=*/ this);
FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
firebaseAppCheck.installAppCheckProviderFactory(PlayIntegrityAppCheckProviderFactory.getInstance());
firebaseAppCheck.installAppCheckProviderFactory(DebugAppCheckProviderFactory.getInstance());
build.gradle
implementation 'com.google.firebase:firebase-appcheck-debug:16.1.1'
is there maybe another way to get the local debug token so i can add it in the firebase console?

Error getting App Check token; using placeholder token instead

My app was working normally since today.I already included Firebase Storage in my Android Flutter App and it works after that today I get AppCheck Errors suddenly. I was not include App Check for our project or not enforced in settings. After that I was following the official documentation for initialization appcheck : https://firebase.flutter.dev/docs/app-check/usage.
This is my Kotlin MainActivity:
import android.os.Bundle
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory
import io.flutter.embedding.android.FlutterActivity
class MainActivity : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
FirebaseApp.initializeApp(/*context=*/ this);
val firebaseAppCheck = FirebaseAppCheck.getInstance()
firebaseAppCheck.installAppCheckProviderFactory(
DebugAppCheckProviderFactory.getInstance())
super.onCreate(savedInstanceState)
}
}
and this is my main():
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate();
runApp(MyApp());
}
I also added this to my app/build.gradle
dependencies {
implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta01'
}
When I make a request to firebase storage, I would expect something like this in my console:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
Instead, I'm getting an error:
2021-11-21 18:11:51.442 2091-3452/com.sekspir.grind4gain W/ExponenentialBackoff: network unavailable, sleeping.
2021-11-21 18:11:53.500 2091-3452/com.sekspir.grind4gain W/StorageUtil: Error getting App Check token; using placeholder token instead. Error: com.google.firebase.FirebaseException: Error returned from API. code: 403 body: App attestation failed.
2021-11-21 18:12:11.136 2091-3633/com.sekspir.grind4gain V/NativeCrypto: SSL handshake aborted: ssl=0xdaa42da8: I/O error during system call, Connection reset by peer
Did I miss something here? I am using a real Android device with flutter debug build.
This is Firestore AppCheck Stats looks both of request income
But in Storage session there are not any request fail or success.
Can you double check that you correctly enabled the enforcement ?
It is required to work with Firebase Storage solutions.
Are you using your app in "PWA" mode or in "Native" mode ?
The Recaptcha may be mandatory depending on how your app is distributed.
await FirebaseAppCheck.instance.activate(
webRecaptchaSiteKey: 'recaptcha-v3-site-key',
);
I'm not sure about your implementation of the appcheck-debug dependency on the Android Native side.
Since it's already implemented by the Flutter library itself, you should remove it.
Personal note : I had troubles with FirebaseAppCheck on some devices, while it was working perfectly on other devices.
This library is still in beta and I would recommend to wait before using it in production.

Issues with Flutter and Phoenix Channels

We're having some issues with our Flutter App and Pheonix Web Sockets. We know that the Backend is working properly, since the JavaScript client is fully functional. But with the Flutter app we can't connect to the server.
final socket = PhoenixSocket("ws://###.##/socket");
connectSocket() async {
await widget.socket.connect();
_channel = widget.socket.channel("test:lobby")
_channel.on("say", _say);
_channel.join();
}
One of the errors were gettig:
I/flutter ( 4227): WebSocket connection to ws://###.##:8080/socket?vsn=2.0.0 failed!: WebSocketException: Connection to 'http://###.##:8080/socket?vsn=2.0.0#' was not upgraded to websocket
On the client side we're using Phoenix Wings, the port and URL are correct.
Why does this keep happening?
Are there any other libraries for Flutter with Phoenix Interactions?
Is there a better documentation for phoenix_wings or for any other packages?
I had it working as
final socket = PhoenixSocket("ws://####.###/socket/websocket");
hth.

HttpClient not found in elastic4s?

I am getting below error while using HttpClient. Can you let me know how to use HttpClient exactly. I am new with elastic4s.
I want to connect scala with ssl configured elasticsearch. I also want to know how I can pass SSL details with link such as keystore path, truststore path and user name , password.
scala> import com.sksamuel.elastic4s.http.{HttpClient, HttpResponse}
import com.sksamuel.elastic4s.http.{HttpClient, HttpResponse}
scala> import com.sksamuel.elastic4s.http.ElasticDsl._
import com.sksamuel.elastic4s.http.ElasticDsl._
scala> val client = HttpClient(ElasticsearchClientUri(uri))
<console>:39: error: not found: value HttpClient
val client = HttpClient(ElasticsearchClientUri(uri))
HttpClient appears to be a trait in the codebase.You seem to be using the same as an object. You can check the implementation Here. For your use case i think the better approach would be to use ElasticClient. Code would look something like this
import com.sksamuel.elastic4s.http._
import com.sksamuel.elastic4s.{ElasticClient, ElasticDsl, ElasticsearchClientUri}
val client = elastic4s.ElasticClient(ElasticsearchClientUri(uri))
I got the same problem, i.e. in my setup I got errors (not found) when trying to use HttpClient (elastic4s-core,elastic4s-http-streams and elastic4s-client-esjava version 7.3.1 on scala 2.12.10).
The solution: you should be able to find and use JavaClient, an implementation of HttpClient that wraps the Elasticsearch Java Rest Client.
An example of how to use the JavaClient can be found here.
Thus, your code should look like the following:
import com.sksamuel.elastic4s.http.JavaClient
import com.sksamuel.elastic4s.{ElasticClient, ElasticDsl, ElasticProperties}
...
val client = ElasticClient(JavaClient(ElasticProperties(uri)))

How do you get logging from the CXF Rest Client?

This took me quite a long time to figure out. I'm asking this question so I can answer it for others:
How do you get useful logging info from the CXF Rest Client? EG: The url, params, payload, response, etc.
Note: This question already exists but it's asking about CXF and Resteasy. I only want the answer for CXF: Logging in CXF and RestEasy clients
Here's how you do it with CXF:
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.WebClient;
import org.json.JSONException;
import org.json.JSONObject;
...
WebClient client = WebClient.create(endPoint, providers).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
ClientConfiguration config = WebClient.getConfig(client);
config.getInInterceptors().add(new LoggingInInterceptor());
config.getOutInterceptors().add(new LoggingOutInterceptor());
Using the JAX-RS 2.0 client from CXF 3.1.x I am having some trouble getting the LoggingInInterceptor and LoggingOutInterceptor to work. I changed to use the LoggingFeature as described at http://cxf.apache.org/docs/message-logging.html and it worked first try:
Client client = ClientBuilder.newBuilder().register(LoggingFeature.class).build()
You can pass directly an instance of LoggingFeature to your client using the appropriate create() method.
Response response = WebClient
.create(baseAddress,
Arrays.<Object>asList(Arrays
.<Object>asList(new JacksonJsonProvider())),
Arrays.<Feature>asList(new LoggingFeature()), null)
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).path(path).post(request);