how to run a websocket server?

11,669

Solution 1

To use websockets, you need to have both browser and a server that support the websocket protocol. You will also need a traditional web server like Apache or IIS to serve your website's static content.

phpws is both a client and server library written in PHP.

Your browser also needs websocket support - which means you are using either Firefox or Chrome (IE doesn't have proper WS support, except in IE10).

Once you have downloaded the websocket server (this is not the same as Apache), you need to run it and make sure its running the background. This means that unless you are using a Windows-native application (like SuperWebSocket), you'll need to make sure not to close the command prompt window!

Once the server is running, you client can connect to it to push messages. This is an example of a client in PHP from the phpws project:

<?php
        require_once("websocket.client.php");

        $input = "Hello World!";
        $msg = WebSocketMessage::create($input);

        $client = new WebSocket("ws://127.0.0.1:12345/echo/");
        $client->open();
        $client->sendMessage($msg);

        // Wait for an incoming message
        $msg = $client->readMessage();

        $client->close();

        echo $msg->getData(); 
?>

Solution 2

this php library can be used to setup a very simple php websocket server

https://github.com/ghedipunk/PHP-Websockets

I have tested, it works.

Solution 3

I dont know where you went wrong so I just put some tips:

  1. I recommend Ratchet as a strong flexible web socket library.
  2. Before you can run php scripts from command prompt you should add php to your Environment.
  3. Install and enable all necessary extensions on WAMP. php_sockets and cURL usually are used.

However after while you will find out it's easier to install virtual machine on your Windows an setup Ubuntu on it.

Share:
11,669
DanWilliams23
Author by

DanWilliams23

Updated on June 04, 2022

Comments

  • DanWilliams23
    DanWilliams23 about 2 years

    I have looked at a a variety of simple tutorials on how make a PHP websocket chat application. I felt this would be a good starting point for looking at websockets. however I seem to be falling at the first hurdle!!

    I have tried running a number source code examples but have had no luck.

    I have a windows 7 OS and I am attempting to run the websocket server using Wamp.

    I have tried running it from the windows command prompt and from the command prompt that comes with wamp.

    What should I be entering into these prompts to get the websocket server up and running?

    I appreciate this question is pretty vague. But if anyone can shed some light on where I am going wrong, I would be very grateful.