Consider removing shelf_web_socket pkg and its usage in client for connecting to server approach.
Background:
A Shelf server (started with shelf_io.serve) is an HTTP server. It expects clients to send proper HTTP requests (with method, headers, path, etc.) and it responds with proper HTTP responses (status code, headers, body).
Socket.connect from dart:io creates a raw TCP socket. It gives you a low-level byte stream with no HTTP protocol handling.
If using Socket.connect to. connect to a Shelf server:
- We can establish the TCP connection.
- But when we send raw bytes, the Shelf handler will usually not understand them as a valid HTTP request → it will likely return 400 Bad Request or close the connection.
| Method |
Recommended? |
Use Case |
Code Style |
http package (http.get, http.post, etc.) |
Best |
Normal REST API, JSON, file upload, etc. |
High-level |
| HttpClient (dart:io) |
Good |
When you don't want extra dependencies |
Medium |
| Socket.connect |
Avoid |
Only if you want raw TCP (not HTTP) |
Low-level |
| WebSocket |
Good |
Real-time bidirectional (use shelf_web_socket on server) |
High-level |
Proposal
→ Use http pkg with its methods for the best fit to server serving currently.
Consider removing
shelf_web_socketpkg and its usage in client for connecting to server approach.Background:
A Shelf server (started with
shelf_io.serve) is an HTTP server. It expects clients to send proper HTTP requests (with method, headers, path, etc.) and it responds with proper HTTP responses (status code, headers, body).Socket.connectfromdart:iocreates a raw TCP socket. It gives you a low-level byte stream with no HTTP protocol handling.If using
Socket.connectto. connect to a Shelf server:http.get,http.post, etc.)shelf_web_socketon server)Proposal
→ Use
httppkg with its methods for the best fit to server serving currently.