PHP WebSocket Bi-directional Transfer

In this article you will learn to execute your bidirectional data transfer in PHP WebSocket.

Create a folder with name say 2-way into your project folder. Inside this folder create two files: client.php and server.php.

  • client.php: The file from where user can send their chat messages also receive server response.
  • server.php: The file which listen user’s messages and also reply to that.

Code Settings for “server.php”

Open server.php and write this following code into it.

<?php
$host = "127.0.0.1"; // hostname
$port = 8085; //port

// Create PHP Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Not connected");

// Bind socket with host and port
$result = socket_bind($socket, $host, $port) or die("Not bind");

// Listen socket from it's address
$result = socket_listen($socket, 3) or die("Not listen");

do {
    // Check messages from client's socket
    $accept = socket_accept($socket) or die("Not accept");

    // Read client message
    $msg = socket_read($accept, 1024) or die("Not read");

    $msg = trim($msg);

    echo "Client: " . $msg;

    echo "\n";

    echo "Enter reply:\n";

    // Enter input/reply to console
    $input = fgets(STDIN);

    // Write message to socket
    socket_write($accept, $input, strlen($input));

    // client Socket close
    socket_close($accept);

} while (true);

// Socket close
socket_close($socket);

You can see above code used several PHP WebSocket functions. This is all about the code which listen to socket for user’s messages and also for prompt reply.

Next, let’s prepare for client.php

Code Settings for “client.php”

Open client.php and write this following code into it.

<?php

$host = "127.0.0.1"; // hostname
$port = 8085; //port

// Create PHP Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Not connected");

// Connect socket with host and port
socket_connect($socket, $host, $port) or die("Not bind");

$msg = "Hello from Client...";

// Data Submit
if (isset($_POST['submit'])) {

    $msg = $_POST['message'];
}

// Write client message to socket
socket_write($socket, $msg, strlen($msg));

// Read socket message
$replyMessage = socket_read($socket, 1024) or die("Not read");

echo "Server: " . $replyMessage;

// Socket close
socket_close($socket);
?>

<form method="post">

   Enter message:<br/>

   <input type="text" name="message">

   <button name="submit">Submit</button>

</form>

Now, everything is done for creating Uni-direction PHP WebSocket program.

Application Testing

Open terminal / command prompt.

Run this command to start server.php

$ php -f server.php

Back to browser and run client.php. Once you run, go back to terminal and see…

Back to client page,

Next, whenever you type any message and click on submit it will pass message to socket. Server then, listen and read message print to console.

Also you can put your reply to user.

It will show you as:

Successfully, you have created your bidirectional program into PHP WebSocket.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more