I'm doing some tests with web_socket_channel Flutter plugin and I've noticed a very strange behavior. I've implemented flutter-dev's example, just changing the socket kind to HtmlWebSocketChannel
in order to make it work in web builds.
If I compile my app with flutter build web --release
and later I expose it with a local webserver, it works perfectly fine. Same happens if I execute it in debug mode.
However, if I deploy the release version to Firebase hosting (firebase deploy
), the widgets where a HtmlWebSocketChannel
is present are rendered as a grey box. If I remove those instances, all widgets are rendered as usual.
I thought Firebase hosting was nothing more that a very simple web server, I can't see how can it interfere with specific widgets in a Flutter app. Maybe the cause is related to the fact I'm accesing a remote URL?
Any help will be appreciated!
Here's the code of the app I'm deploying:
import 'package:flutter/foundation.dart';
import 'package:web_socket_channel/html.dart';
import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final title = 'WebSocket Demo';
return MaterialApp(
title: title,
home: MyHomePage(
title: title,
channel: HtmlWebSocketChannel.connect('ws://echo.websocket.org'),
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
final WebSocketChannel channel;
MyHomePage({Key key, @required this.title, @required this.channel})
: super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Form(
child: TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Send a message'),
),
),
StreamBuilder(
stream: widget.channel.stream,
builder: (context, snapshot) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 24.0),
child: Text(snapshot.hasData ? '${snapshot.data}' : ''),
);
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
widget.channel.sink.add(_controller.text);
}
}
@override
void dispose() {
widget.channel.sink.close();
super.dispose();
}
}
As mentioned above in my comments this seems to be an issue with trying to access insecure resource from a secure environment as https
. Here is a working demo of the same code you used.
https://stackoverlfow-demos.web.app/#/
I just replaced it with wss
and deployed it to the firebase hosting.
channel: HtmlWebSocketChannel.connect('wss://echo.websocket.org'),