Giter Site home page Giter Site logo

yoanesber / php-ratchet Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 1.0 2.55 MB

This simple project aims to develop a simple chat application using PHP programming language that implement Ratchet Web socket.

PHP 3.82% CSS 96.18%
php ratchet ratchet-php-server chat-application web websocket

php-ratchet's Introduction

Ratchet - PHP Websocket

This simple project aims to develop a simple chat application using PHP programming language that implement Ratchet Web socket. The chat will accept all incoming messages and deliver that message to all other connections.

Preparing application

You only need to do these once for all.

  1. Make a folder for your application using command prompt:

    >> cd E:\xampp\htdocs
    >> E:\xampp\htdocs>mkdir PHP-Ratchet
    >> E:\xampp\htdocs>cd PHP-Ratchet
    >> E:\xampp\htdocs\PHP-Ratchet>
    
  2. Install Ratchet using composer :

    >> E:\xampp\htdocs\PHP-Ratchet>composer require cboden/ratchet
    
  3. We'll start off by creating a class, Chat class. This class will be our chat "application". This basic application will listen for 4 events:

    <ul>
    <li>onOpen - Called when a new client has Connected</li>
    <li>onMessage - Called when a message is received by a Connection</li>
    <li>onClose - Called when a Connection is closed</li>
    <li>onError - Called when an error occurs on a Connection</li>
    </ul>
    

Save this class as src/Chat.php. We're going to hold everything in the MyApp namespace. Our Chat class will be our application logic.

  1. Add or Modify composer.json like this:

    {
         "autoload": {
             "psr-4": {
                 "MyApp\\": "src"
             }
         },
         "require": {
             "cboden/ratchet": "^0.4"
         }
     }
    
  2. Next, we're going to create script/file will call from the command line to launch our application:

    <?php
     use Ratchet\Server\IoServer;
     use Ratchet\Http\HttpServer;
     use Ratchet\WebSocket\WsServer;
     use MyApp\Chat;
    
     require dirname(__DIR__) . '/vendor/autoload.php';
     define("PORT", 8182);
    
     $server = IoServer::factory(
         new HttpServer(
             new WsServer(
                 new Chat()
             )
         ),
         PORT
     );
    
     $server->run();
    

Above, you'll see we create an I/O (Input/Output) server class that listening for any incoming requests on port 8182. Save this script as src/chat-server.php.

  1. Now, we're going to create chat interface and save this as src/index.php. You can use bootstrap for the page template (You can copy from my src/index.php):

    >> E:\xampp\htdocs\PHP-Ratchet>composer require twbs/bootstrap:4.0.0
    
  2. Run composer update using this statement:

    >> E:\xampp\htdocs\PHP-Ratchet>composer update
    
  3. Now, we can run the server with the following command in your terminal:

    >> E:\xampp\htdocs\_Project\PHP-Ratchet>cd src
    >> E:\xampp\htdocs\_Project\PHP-Ratchet\src>php chat-server.php
    
  4. And run http://localhost/_Project/PHP-Ratchet/src/index.php on your browser to access chat interface.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.