2017 © Pedro Peláez
 

library socket

Laravel library for asynchronously serving WebSockets.

image

orchid/socket

Laravel library for asynchronously serving WebSockets.

  • Thursday, May 31, 2018
  • by tabuna
  • Repository
  • 19 Watchers
  • 193 Stars
  • 11,005 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 30 Forks
  • 6 Open issues
  • 18 Versions
  • 13 % Grown

The README.md

Warning this repository is no longer supported

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


, (*1)

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. , (*2)

, (*3)

Installation Laravel WebSocket

install package, (*4)

$ composer require orchid/socket

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

Orchid\Socket\SocketServiceProvider::class

structure, (*6)

php artisan vendor:publish

Usage

Create socket listener:

To create a new listener, you need to, (*7)

php artisan make:socket MyClass

In the folder app/Http/Sockets create template Web listener socket, (*8)

After creating a need to establish a route which Is located routes/socket.php, (*9)

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

To launch the web-socket, use the command:, (*10)

php artisan socket:serve

FAQ

JavaScript

Connecting Web socket in JavaScript, (*11)

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, (*12)


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., (*13)

The Versions

31/05 2018

dev-master

9999999-dev

Laravel library for asynchronously serving WebSockets.

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

18/10 2017

1.6

1.6.0.0

Laravel library for asynchronously serving WebSockets.

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

08/09 2017

1.5.3

1.5.3.0

Laravel library for asynchronously serving WebSockets.

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

08/09 2017

1.5.2

1.5.2.0

Laravel library for asynchronously serving WebSockets.

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

29/01 2017

1.5.1

1.5.1.0

Laravel library for asynchronously serving WebSockets.

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

24/08 2016

1.5

1.5.0.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

01/08 2016

1.4.4.1

1.4.4.1

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

01/08 2016

1.4.4

1.4.4.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

27/05 2016

1.4.3

1.4.3.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php server web websocket socket

24/12 2015

1.4.2

1.4.2.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

24/12 2015

1.4.1

1.4.1.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

17/12 2015

1.3.0

1.3.0.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

17/12 2015

1.4

1.4.0.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

17/12 2015

1.2.4

1.2.4.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

16/12 2015

1.0.2

1.0.2.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

16/12 2015

1.0.2.1

1.0.2.1

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

16/12 2015

1.2.3

1.2.3.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket

16/12 2015

1.0.1

1.0.1.0

Simple WebSockets for Laravel

  Sources   Download

MIT

The Requires

 

by Avatar tabuna

laravel framework php web websocket socket