How to use WebSocket in XAMPP server

Viewed 12749

How to enable websocket in xampp server? I use windows 10. I want to build a chat based on websocket and but I can't connect to the websocket.

I found this article http://phppot.com/php/simple-php-chat-using-websocket/ but I don't understand how to connect php xampp to websocket.

4 Answers

Uncomment extension=php_sockets.dll in php.ini file to enable websockets in php.

I tried get to run the example too, with the same error. Following solution is working:

download the example code and copy to your htdocs folder.

open two tabs in your Browser. 1. run the php-socket.php and let it load. 2. run the index.php in the 2nd tab and the example will work.

I made some changes and got it working because I already have a website running on XAMPP.

  1. "php-socket.php" → Change line 3:

define('PORT',"8090");

to:

define('PORT',"1337");

  1. "php-socket.php" → Change line 11:

socket_bind($socketResource, 0, PORT);

to:

socket_bind($socketResource, HOST_NAME, PORT);

  1. "index.php" → Change line 25:

var websocket = new WebSocket("ws://localhost:8090/demo/php-socket.php");

to:

var websocket = new WebSocket("ws://localhost:1337/demo/php-socket.php");

  1. Make a .bat file inside this demo folder:
@echo off
"C:\xampp\php\php.exe" -q "C:\xampp\htdocs\demo\php-socket.php"
  1. Launch the .bat file, and keep the cmd open as this is the socket listener or server; as soon as you close it, the frontend will say "Connection Closed"
Related