Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter web socket connection problem on wasm #404

Open
Flattih opened this issue Dec 18, 2024 · 0 comments
Open

Flutter web socket connection problem on wasm #404

Flattih opened this issue Dec 18, 2024 · 0 comments

Comments

@Flattih
Copy link

Flattih commented Dec 18, 2024

Update: flutter run -d chrome works fine.
But it doesn't work with web assembly flutter run -d chrome --wasm

Update 2: WASM works on production.

socket_io_client: ^3.0.2
socket.io: 4.8.1 nodejs
Flutter version 3.24.5
Dart version 3.5.4
Nodejs version 18.17.0

I cannot connect to the socket on the web, it works fine on Android and iOS. I'm trying to establish a socket connection with flutter web for the first time. Is there something I don't know? I leave my client and server codes below. It gives no transport available error on the web.

NodeJS

const io = require("socket.io")(server, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"],
    transports: ["websocket"],
    
  },
});
io.on("connection", (socket) => {
  socket.on("setup", (userId) => {
    console.log("User online setup ", userId);
    socket.join(userId);
    if (
      userId !== null &&
      userId !== undefined &&
      users.indexOf(userId) === -1
    ) {
      users.push(userId);
      console.log("pushed user", userId);
      io.emit("online", users);
    }
  });
}

DART

class SocketHelper {
  io.Socket? socket;
  static SocketHelper? _instance;

  SocketHelper._internal() {
    socket = io.io("url", <String, dynamic>{
      'transports': ['websocket'],
      'autoConnect': false,
    });
    socket!.connect();
  }

  static SocketHelper get instance {
    _instance ??= SocketHelper._internal();
    return _instance!;
  }

  void setup(WidgetRef ref) {
    socket?.emit("setup", ref.read(userProvider)?.id);
  }
@Flattih Flattih changed the title Flutter web socket connection problem Flutter web socket connection problem on wasm Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant