Skip to content

Consider removing Socket.connect from client connect method #109

@huynguyennovem

Description

@huynguyennovem

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:

  1. We can establish the TCP connection.
  2. 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.

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions