Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Socket Laravel Package

amphp/socket

Non-blocking, event-driven socket library for PHP using Amp. Provides async TCP/UDP sockets, client/server connections, DNS and TLS support, timeouts, cancellation, and backpressure-friendly streams—ideal for high-concurrency network services and daemons.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer: composer require amphp/socket. Then, ensure the Amp event loop is initialized — usually via Amp\Loop::run(). The first practical use case is building a simple async TCP server: use ServerListenContext to bind a port and handle incoming connections with accept() inside the loop. For clients, use connect() on a hostname/port and pipe data via ReadableResourceStream/WritableResourceStream. Minimal boilerplate is required: just create the socket context, await connections/data, and let Amp manage non-blocking I/O.

Implementation Patterns

  • Protocol Servers: Combine with amphp/artifacts or custom framing logic (e.g., length-prefixed messages) to implement custom protocols like WebSocket or Redis-like stacks. Use StreamSocket for bidirectional I/O and on('close') hooks for cleanup.
  • Concurrent Clients: Spawn multiple connect() calls in parallel using Amp\Promise\all() or foreach loops with await inside Loop::run(). Leverage cancellation tokens via Amp\Promise\cancel() to abort timed-out connections.
  • TLS Security: Wrap listeners/clients with SslServerContext or SslClientContext using Amp\Socket\connectTls()/Amp\Socket\listenTls(). Always validate certificates in production (e.g., verify peer CN/SAN via context_options).
  • Backpressure Handling: Read data incrementally using ReadableResourceStream::read() in a loop, and pause/resume streams via pause()/resume() based on downstream buffer levels to prevent memory overflow.

Gotchas and Tips

  • Missing Loop Initialization: Forgetting to call Loop::run() or using blocking I/O (e.g., echo, var_dump, sleep()) inside async code will halt the event loop — use Loop::defer() or Loop::delay() instead.
  • Stream Closing Behavior: Sockets auto-close on destruct, but failing to explicitly close() or cancel() long-running operations (e.g., accept()) can leak resources — always tie cancel() to request lifecycle or timeouts.
  • TLS Certificate Validation: By default, connectTls() skips peer verification in some environments — explicitly configure verify_peer/verify_peer_name in the context options for production.
  • Port Binding Conflicts: If ServerListenContext fails with "Address already in use", ensure no previous instance is still running (especially during development) and check SO_REUSEADDR via stream context options.
  • Extensibility: Extend StreamSocket or wrap streams (e.g., with Amp\ByteStream\Filter) to implement custom middleware like logging, rate limiting, or compression — but avoid blocking operations in stream filters.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport