orchid/socket

This package is abandoned and no longer maintained. No replacement package was suggested.

Laravel library for asynchronously serving WebSockets.

1.7 2018-10-31 19:46 UTC

This package is auto-updated.

Last update: 2020-05-27 08:56:58 UTC


README

If you are looking for a good way to use laravel's web socket please look: https://github.com/beyondcode/laravel-websockets

ecfae2ce-e5c5-11e6-8430-16eef73c01a5.png

Laravel library for asynchronously serving WebSockets.
Build up your application through simple interfaces and re-use your application without changing any of its code just by combining different components.

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f6e6174652d50617950616c2d677265656e2e737667 68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30343464393735642d633933342d346432612d396662302d6666646630363564623534352f6d696e692e706e673f32 68747470733a2f2f7374796c6563692e696f2f7265706f732f34383130353037312f736869656c643f6272616e63683d6d6173746572 68747470733a2f2f706f7365722e707567782e6f72672f6f72636869642f736f636b65742f762f737461626c65 68747470733a2f2f706f7365722e707567782e6f72672f6f72636869642f736f636b65742f646f776e6c6f616473 68747470733a2f2f706f7365722e707567782e6f72672f6f72636869642f736f636b65742f6c6963656e7365

Installation Laravel WebSocket

install package

$ composer require orchid/socket

edit config/app.php service provider : (Laravel < 5.5)

Orchid\Socket\SocketServiceProvider::class

structure

php artisan vendor:publish

Usage

Create socket listener:

To create a new listener, you need to

php artisan make:socket MyClass

In the folder app/Http/Sockets create template Web listener socket

After creating a need to establish a route which Is located routes/socket.php

//routing is based on an Symfony Routing Component
$socket->route('/myclass', new MyClass, ['*']);

To launch the web-socket, use the command:

php artisan socket:serve

FAQ

JavaScript

Connecting Web socket in JavaScript

var socket = new WebSocket("ws://localhost");

socket.onopen = function() {
  alert("The connection is established.");
};

socket.onclose = function(event) {
  if (event.wasClean) {
    alert('Connection closed cleanly');
  } else {
    alert('Broken connections'); 
  }
  alert('Key: ' + event.code + ' cause: ' + event.reason);
};

socket.onmessage = function(event) {
  alert("The data " + event.data);
};

socket.onerror = function(error) {
  alert("Error " + error.message);
};


//To send data using the method socket.send(data).

//For example, the line:
socket.send("Hello");

Authorization

Example of installation numbers unique socket and session laravel

public function onOpen(ConnectionInterface $conn)
{
    $this->clients->attach($conn);
    
    //take user id
    $userId = $this->getUserFromSession($conn);
    
    //Create a list of users connected to the server
    array_push($this->userList, $userId);
    
    //We tell everything that happened
    echo "New connection! user_id = ({$userId})\n";
}

public function getUserFromSession($conn)
{
    // Create a new session handler for this client
    $session = (new SessionManager(App::getInstance()))->driver();
    
    // fix issue https://github.com/laravel/framework/issues/24364
    if (Config::get('session.driver') == 'file') {	
	clearstatcache();
    }
    
    // Get the cookies
    $cookies = $conn->WebSocket->request->getCookies();
    
    // Get the laravel's one
    $laravelCookie = urldecode($cookies[Config::get('session.cookie')]);
    
    // get the user session id from it
    $idSession = Crypt::decrypt($laravelCookie);
    
    // Set the session id to the session handler
    $session->setId($idSession);
    
    // Bind the session handler to the client connection
    $conn->session = $session;
    $conn->session->start();
    
    //We take the user from a session
    $userId = $conn->session->get(Auth::getName());
    return $userId;
}

Nginx proxy

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    upstream websocket {
        server you-web-site.com:5300;
    }

    server {
        listen 443;
        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
	    proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_set_header X-Forwarded-Proto https;
            proxy_redirect off;
        }
    }

Supervisor

[program:laravel-socket]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/your-path/artisan socket:serve
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/your-path/storage/logs/socket.log

License

The MIT License (MIT). Please see License File for more information.