Reputation: 129
I'm working on a chat application using the Ratchet package. How can I run command "chat:serve" on Shared Hosting? I need to run command without console line. I tried this:
Artisan::call('chat:serve');
or this:
$serve = new WSChatServer();
$serve->fire();
but it does not work. Web page never stops loading. I need to run this Artisan command in the background and it should be running all the time. How do I do it? Is it possible to do this without VPS hosting?
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Chat;
class WSChatServer extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'chat:serve';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Start chat server.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$port = intval($this->option('port'));
$this->info("Starting chat web socket server on port " . $port);
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
$port
);
$server->run();
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
];
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['port', 'p', InputOption::VALUE_OPTIONAL, 'Port where to launch the server.', 9090],
];
}
}
Upvotes: 0
Views: 2030
Reputation: 296
Your question hints at where you are going to have the most difficulty:
"Is it possible to do this without VPS hosting?"
Some of the options to solve this problem may be blocked by your shared host. Fundamentally each call from your webserver to a php application is going to start a php process. When it is finished that process returns. Except you want this process to not be finished but you want the web request to return. So you really have three options:
1) Have php spawn a new process to run your artisan command and then have the original one return. To do that you need access to one of the process extensions in php. Most shared hosts disable this by default. You also need to ensure that the process is created in such a way as not to stop when parent process is closed.
2) You can login via ssh and run the command. Same as before you need to ensure the process is spawned so it will not stop when you exit the SSH connection. Nohup is a helpful process there.
3) You can write a cron script that launches the background process if one is not running. Similar to the ssh command this uses something else to trigger the process.
I would note that many shared webhosts don't allow long running user processes and thus might not be the right solution for this project.
Upvotes: 1