Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:05:48+00:00 2026-05-31T09:05:48+00:00

I need to debug a socket connection that my PHP frontend makes to a

  • 0

I need to debug a socket connection that my PHP frontend makes to a backend service on another host, and I need to do this as close to the metal as possible. I ‘ve already abstracted the connection at various levels, which among others gives me the capability to easily attach stream filters to the connection. So the problem should be very easy to solve: package a stream filter into a class which allows arbitrary callbacks to be executed when data is sent or received, and then for example append these to a couple of log files.

The only trouble is that my (naive?) expectation that there should be some such filter implementation floating online appears to be wrong! I don’t mind writing the filter myself, but maybe there is something available that I just didn’t manage to find?

For reference, I did google many obvious variations on “php logging stream filter”.

Update: To clarify, what I ‘m looking for is something that would allow me to write code morally equivalent to this:

$params = array(
    'onDataSent' => function($data) { echo "Sent: $data\n"; },
    'onDataReceived' => function($data) { echo "Received: $data\n"; },
);

stream_filter_register('logging', 'HookableStreamFilter');
stream_filter_append($someStream, 'logging', STREAM_FILTER_ALL, $params);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:05:49+00:00Added an answer on May 31, 2026 at 9:05 am

    For posterity, here’s what I ended whipping up. The filter itself derives from php_user_filter, which is described in the docs for stream_filter_register:

    class HookableFilter extends php_user_filter {
        public function filter($in, $out, &$consumed, $closing) {
            $data = '';
    
            while ($bucket = stream_bucket_make_writeable($in)) {
                $consumed += $bucket->datalen;
                $data .= $bucket->data;
                stream_bucket_append($out, $bucket);
            }
    
            call_user_func($this->params, $data);
            return PSFS_PASS_ON;
        }
    }
    

    Note that I ‘m using directly $this->params as the callback, making this really spartan in function (there’s a reason; see below).

    Registering the filter with PHP:

    stream_filter_register('generic.hookable', 'HookableFilter');
    

    Attaching the filter to the stream:

    $callback = function($data) { /* log the data */ };
    stream_filter_append($stream, 'generic.hookable', STREAM_FILTER_READ, $callback);
    stream_filter_append($stream, 'generic.hookable', STREAM_FILTER_WRITE, $callback);
    

    Important: As far as I was able to see, if you attach a filter to both channels of a duplex stream (e.g. one produced with stream_socket_client) there is no way for your filter to know on which channel it’s operating when it is invoked. Therefore if you need to differentiate between incoming and outgoing data as I was, the only option is to attach the filter separately to each channel. If you do, you will also certainly want to supply a different callback for each channel (not the same as done in the simplified example above).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need debug some old code that uses a Hashtable to store response from
I need to debug a class library project that is provided to the main
I have a process that spawns a helper process. Sometimes I need to debug
I need to pass -Dlog4j.debug to tomcat through command line, how to do that
I've inherited some code that I need to debug. It isn't working at present.
We're having problems with UTF-8 in Solr, and need to debug the documents that
I need to debug a situation that happens when no network connectivity is present
I've read in different places that this is possible. I do not need to
This is a Windows Console application (actually a service) that a previous guy built
When you need to debug a Website that hosts on IIS Express, you usually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.