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

  • Home
  • SEARCH
  • 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 7890051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:15:13+00:00 2026-06-03T06:15:13+00:00

I’m trying to make my PHP server a bit more efficient. I’ve built an

  • 0

I’m trying to make my PHP server a bit more efficient.
I’ve built an object named Client which contains the connected client (which has an open socket connection with the server) information such as name, id etc.

For now I have one array of socket connections, and one array of Client objects.
When I’m referring a connection, I’m searching inside my Client array to find the right client who matches this connection.
It works great, but it’s a bit inefficient.. For small amount of clients in the server you don’t feel it, but I’m afraid that if I’ll have thousands of connection it will slow down the server.

As a solution I thought about 2 dimensional array, but I have a logic problem designing it.

Can I do something like this:

$clients = array();
$temp = array($newsock, new Client());
$clients[] = $temp;

I want my $clients[] to be the socket and the $clients[][] to be the client object.
In each row of $client I will have only $client[$index][0] which will be my client object for that connection.
Will I be able to send this to the socket_select() function?

  • 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-06-03T06:15:14+00:00Added an answer on June 3, 2026 at 6:15 am

    You say that you have within your client object an id attribute. Why not use that id as the key for both arrays?

    • Socket connections array
    • Client object array

    You might even be able to hold the connection and the client object in one array, each in one object under the same key I talked about before – the clients id.

    In any case, wherever you decide to store your clients connection object, you will be able to pass it to all the relevant socket functions –

    • socket_select();
    • socket_accept();
    • socket_write();
    • etc…

    With regard to the efficiency of your server, I implemented some forking for broadcasting data to large amounts of clients (all of them in the example of a chat server).

    This is the implementation that I used for forking the broadcasts –

    function broadcastData($socketArray, $data){
            global $db;
            $pid = pcntl_fork();
            if($pid == -1) {
                    // Something went wrong (handle errors here)
                    // Log error, email the admin, pull emergency stop, etc...
                    echo "Could not fork()!!";
            } elseif($pid == 0) {
                    // This part is only executed in the child
                    foreach($socketArray AS $socket) {
                            // There's more happening here but the essence is this
                            socket_write($socket,$msg,strlen($msg));
                            // TODO : Consider additional forking here for each client. 
                    }
                    // This is where the signal is fired
                    exit(0);
            }
            // The child process is now occupying the same database 
            // connection as its parent (in my case mysql). We have to
            // reinitialize the parent's DB connection in order to continue using it. 
            $db = dbEngine::factory(_dbEngine); 
    }
    

    The code above was lifted from a previous question of mine (that was self answered).
    Terminating zombie child processes forked from socket server

    Perhaps it might assist you if you chose to start forking processes.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.