PHPackages                             rtm-ctrlz/keepalive - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rtm-ctrlz/keepalive

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rtm-ctrlz/keepalive
===================

TCP-Keepalive fine-tuning utility

0.3.2(4y ago)13.6kMITPHPPHP ^7.2 || ^8.0CI failing

Since Feb 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/rtm-ctrlz/keepalive)[ Packagist](https://packagist.org/packages/rtm-ctrlz/keepalive)[ Docs](https://github.com/rtm-ctrlz/keepalive)[ RSS](/packages/rtm-ctrlz-keepalive/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

Keepalive
=========

[](#keepalive)

This is a helper for setting proper TCP-Keepalive options and values.

Reason
======

[](#reason)

Common way to enable TCP-Keepalive on a socket looks like this:

```
socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
```

And yes, it enables keepalive, but what are keepalive parameters? Example above will use system-default values!

Most systems (OSes) have the following defaults:

OptionDefault valueDescriptionSO\_KEEPALIVE0TCP Keep-Alive is disabledTCP\_KEEPIDLE7200Start keepalive probes after this period (2 hours)TCP\_KEEPINTVL75Interval between keepalive probes (75 seconds)TCP\_KEEPCNT8Number of keepalive probes before deathSo, with default values, first TCP-Keepalive packet will be sent only in 2 hours after last packet, then wait more up to 10 minutes (`75s * 8`) before dropping dead connection.

Is this really your case?

Not for me, I'd like drop connection within 1 minute or less!

Install
=======

[](#install)

```
composer require rtm-ctrlz/keepalive
```

Usage
=====

[](#usage)

> Below you can find some examples (socket/stream/ssl-stream) for "client-side", but same operations could be done for "server-side".

In examples below we use following parameters:

- idle time: 45s
- probes interval: 5s
- number of probes: 3

So we'd get maximum waiting time of 1 minute.

### Raw sockets

[](#raw-sockets)

```
// create socket
$socket = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);
```

### Streams (tcp)

[](#streams-tcp)

```
// create stream
$stream = \stream_socket_client('tcp://127.0.0.1:80);
// get socket
$socket = \socket_import_stream($stream);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);
```

### Streams (ssl)

[](#streams-ssl)

This will be a bit harder, because `socket_import_stream` can not import socket.

But we can do a trick:

- create tcp-connection ([stream\_socket\_client()](https://www.php.net/manual/en/function.stream-socket-client.php))
- import socket ([socket\_import\_stream()](https://www.php.net/manual/en/function.socket-import-stream.php))
- enable tcp-keepalive
- enable encryption ([stream\_socket\_enable\_crypto()](https://www.php.net/manual/en/function.stream-socket-enable-crypto.php))

```
// create stream
// NOTE: stream has "tcp" proto
$stream = \stream_socket_client(
    'tcp://127.0.0.1:443',         // target
    $errno,                        // error number
    $errstr,                       // error description
    1.1,                           // timeout
    STREAM_CLIENT_CONNECT,         // flags
    stream_context_create(         // context
        [
            'ssl' => [
                // ... ssl options
            ],
        ]
    )
);

// get socket
$socket = \socket_import_stream($stream);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);
// enable encryption
\stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
```

### More examples

[](#more-examples)

See [examples](./examples/) directory for more examples.

Gotchas
=======

[](#gotchas)

### Option numbers

[](#option-numbers)

Unfortunately PHP (and ext-sockets) doesn't have `TCP_KEEPIDLE`, `TCP_KEEPINTVL` and `TCP_KEEPCNT` constants.

#### Found values

[](#found-values)

OptionLinuxDarwinBSDTCP\_KEEPIDLE`4``16``256`TCP\_KEEPINTVL`5``257``512`TCP\_KEEPCNT`6``258``1024`Linux: Linux [tcp.h](https://github.com/torvalds/linux/blob/master/include/uapi/linux/tcp.h)Darwin: Darwin XNU [tcp.h](https://github.com/apple/darwin-xnu/blob/master/bsd/netinet/tcp.h)BSD: FreeBSD [tcp.h](https://github.com/freebsd/freebsd/blob/master/sys/netinet/tcp.h)

#### Windows support

[](#windows-support)

Well, Windows should have support for TCP-Keepalive options, but:

- I have no Windows machine to run tests
- I didn't look for `TCP_KEEP*` values on Windows

Feel free to make a PR ;)

Tested with
===========

[](#tested-with)

- [phpstan/phpstan](https://github.com/phpstan/phpstan)
- [squizlabs/PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~75 days

Total

5

Last Release

1616d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4123596?v=4)[Ilia Urvachev](/maintainers/rtm-ctrlz)[@rtm-ctrlz](https://github.com/rtm-ctrlz)

---

Top Contributors

[![rtm-ctrlz](https://avatars.githubusercontent.com/u/4123596?v=4)](https://github.com/rtm-ctrlz "rtm-ctrlz (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

keepalivephpsocketssltcptimeouttls

###  Code Quality

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rtm-ctrlz-keepalive/health.svg)

```
[![Health](https://phpackages.com/badges/rtm-ctrlz-keepalive/health.svg)](https://phpackages.com/packages/rtm-ctrlz-keepalive)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
