felangel / web_socket_client

A simple WebSocket client for Dart which includes automatic reconnection logic.
https://pub.dev/packages/web_socket_client
MIT License
150 stars 32 forks source link

application doesn't work properly in production env #35

Closed macik1423 closed 3 months ago

macik1423 commented 8 months ago

I have application which is running in andorid (flutter). I have server part in .Net. Everything is working fine and I want to implement simple websocket so I use web_socket_client like below:

Future<void> main() async {
  final appVersion = await getVersion();
  final backoff = LinearBackoff(
    initial: const Duration(seconds: 0),
    increment: const Duration(seconds: 1),
    maximum: const Duration(seconds: 5),
  );
  final socket = WebSocket(
    Uri.parse('ws://my_api_address/ws'),
    backoff: backoff,
  );

  socket.connection.listen((event) {
    if (event is Connected ||
        event is Reconnecting ||
        event is Reconnected ||
        event is Connecting) {
      runApp(
        StreamBuilder(
          stream: socket.messages,
          builder: (context, snapshot) {
            if (snapshot.hasData && snapshot.data != appVersion) {
              return RequiredVersionAlert(
                  appVersion: appVersion, requiredVersion: snapshot.data);
            }
            return const App();
          },
        ),
      );
    }
  });
}

There are two streams, first one for connection with server status, second one for message from socket. In the local env everything is forking fine, I didn't spot any issues, but if I try to run in production env (.net app is deploing via IIS windows 10) there is something strange, the client (flutter) performance is very bad, application is stuttering and reacting very slow. There is my code for websocket in server:

public class WebSocketController : ControllerBase
{
    private const string version = "3.0.0";
    [HttpGet("/ws")]
    public async Task Get()
    {
        if (HttpContext.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
            await Echo(webSocket);
        }
        else
        {
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
        }
    }

    private static async Task Echo(WebSocket webSocket)
    {
        while (webSocket.State == WebSocketState.Open)
        {
            var versionBytes = Encoding.UTF8.GetBytes(version);
            await webSocket.SendAsync(new ArraySegment<byte>(versionBytes), WebSocketMessageType.Text, true, CancellationToken.None);
        }

        await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
    }
}

The websocket goal is simple, it's just sending application version string. Maybe there is some problem with server settings (firewall etc.) or LinearBackoff settings?

felangel commented 7 months ago

Hi @macik1423 👋 Thanks for opening an issue!

Are you able to share a link to a minimal reproduction sample? It's hard to help pinpoint the issue without being able to reproduce the issue locally.

macik1423 commented 7 months ago

Hello! Thanks for response. Unfortunately I can't reproduce example because this issue exists in prodution env only. When I test on local env everything working fine, so my question was about settings LinearBackoff or firewall may cause this issue somehow?

felangel commented 3 months ago

It could definitely be related to firewall settings. You can rule out the linear backoff by switching to a constant backoff instead to see if the issue persists.

Closing for now since it's been a while and there don't seem to be any actionable next steps. If this is still an issue feel free to comment with any additional context and I'm happy to continue the conversation 👍