MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP WebSockets


Usage of socket extension implements a low-level interface to the socket communication functions based on the popular BSD sockets, providing the possibility to act as a socket server as well as a client.

Simple TCP/IP server



Minimal example based on PHP manual example found here: http://php.net/manual/en/sockets.examples.php Create a websocket script that listens to Port 5000 Use putty, terminal to run telnet 127.0.0.1 5000 (localhost).

This script replies with the message you sent (as a ping-back)

<?php
set_time_limit(0); // disable timeout
ob_implicit_flush(); // disable output caching
// Settings
$address = '127.0.0.1';
$port = 5000;
/*
 function socket_create ( int $domain , int $type , int $protocol )
 $domain can be AF_INET, AF_INET6 for IPV6 , AF_UNIX for Local communication protocol
 $protocol can be SOL_TCP, SOL_UDP (TCP/UDP)
 @returns true on success
*/
if (($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
 echo "Couldn't create socket".socket_strerror(socket_last_error())."\n";
}
/*
 socket_bind ( resource $socket , string $address [, int $port = 0 ] )
 Bind socket to listen to address and port
*/
if (socket_bind($socket, $address, $port) === false) {
 echo "Bind Error ".socket_strerror(socket_last_error($sock)) ."\n";
}
if (socket_listen($socket, 5) === false) {
 echo "Listen Failed ".socket_strerror(socket_last_error($socket)) . "\n";
}
do {
 if (($msgsock = socket_accept($socket)) === false) {
 echo "Error: socket_accept: " . socket_strerror(socket_last_error($socket)) . "\n";
 break;
 }
 /* Send Welcome message. */
 $msg = "\nPHP Websocket \n";
 // Listen to user input
 do {
 if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
 echo "socket read error: ".socket_strerror(socket_last_error($msgsock)) . "\n";
 break 2;
 }
 if (!$buf = trim($buf)) {
 continue;
 }
 // Reply to user with their message
 $talkback = "PHP: You said '$buf'.\n";
 socket_write($msgsock, $talkback, strlen($talkback));
 // Print message in terminal
 echo "$buf\n";

 } while (true);
 socket_close($msgsock);
} while (true);
socket_close($socket);
?>


Conclusion

In this page (written and validated by ) you learned about PHP WebSockets . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Built in server.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.