Flutter websocket with Spring-boot backend

4,095

After a bit of consideration, I found the issue:

The problem is that my spring-boot server was on localhost, but the flutter (which is also the android emulator) has its own loopback service. So calling localhost inside the Flutter program is referring to another place instead of the one I wanted.

I've substituted the localhost for the ip 10.0.2.2 which is an alias to the host PC which is set up to help in development.

For more infos check this answer: here

Of course if you wanted to test from a real device than one would need to publish the backend for the outside, so this answer might be better: here

Share:
4,095
fditz
Author by

fditz

Technology and Science enthusiast. I love to learn new things and wish the day had more than 24 Hours to do so. I really enjoy programming and hope I can help others to solve some of the hurdles I found along the way.

Updated on December 12, 2022

Comments

  • fditz
    fditz over 1 year

    Alright, Flutter has the WebSocket recipe in the cookbook (here). And that works great against the websocket.org test server.

    The thing is I want to connect with my own WebSocket server. So I first used this tutorial from SpringBoot.

    Trying to make a request from the app (I am using the emulator here) to the spring boot backend did not work. I then started tinkering and removed STOMP from the spring boot backend and left it with a simple WebSocket passing strings. It works when using postman or even a webpage but it doesn't work from the app

    The current state is present at this GitHub (both the spring boot and flutter projects): https://github.com/Flavsditz/websocket_sandbox

    Does anyone have any tips here?

    I appreciate it!

  • fditz
    fditz almost 5 years
    Thanks for pointing that out, but that was indeed just a typo when I was preparing the example.