I am trying to create a chat application using Socket and using Swoole as backend. I successfully create a connection to server-client but the issue I am facing now is that whenever I close terminal WebSocket is unable to connect.
Server code:-
<?php
//Create the websocket server object
$websocket_server = new swoole_websocket_server("MY_IP", 3000);
// Register function of the opening connection event
$websocket_server->on('open', function($websocket_server, $request){
var_dump($request->fd, $request->get, $request->server);
$websocket_server->push($request->fd, "Hello welcome\n");
});
// Register function of the receiving message event
$websocket_server->on('message', function($websocket_server, $frame){
echo "Message : {$frame->data}\n";
$websocket_server->push($frame->fd, "Server : {$frame->data}");
});
// Register function of the close event
$websocket_server->on('close', function($websocket_server, $fd){
echo "client_{$fd} is closed\n";
});
// Start the server
$websocket_server->start();
Client side Code:-
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script type="text/javascript">
var wsServer = 'ws://IP:3000';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log("Connected to WebSocket server.");
};
websocket.onclose = function (evt) {
console.log("Disconnected");
};
websocket.onmessage = function (evt) {
console.log('Retrieved data from server: ' + evt.data);
};
websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};
</script>
</body>
</html>
Everything is working fine the only issue is when we close terminal web socket is down. Is there any way we can keep running command in background?.
@anehkumar Please set as a daemon.
$websocket_server = new swoole_websocket_server("MY_IP", 3000);
$websocket_server->set([
'daemonize' => true,
]);
Hi, after implement the above code it was working fine but after some time swoole server closed automatically and we need to login to the terminal free specific port used for swoole and need to restart again.
There is very little documentation about daemonization of swoole and how to handle it the right way. I.e. how do we run it as a service "forever", auto-starting on reboot, and restarting on memory leaks?
Start reading here https://wiki.swoole.com/wiki/index/prid-1
and you should give informations about your os. Running forever is managed by OS
auto-starting on reboot
is never done by software itself - you can script that.
Running as _daemon_ is software.
restarting on memory leaks
Sure?
But, in chinese docs is a good start here https://wiki.swoole.com/wiki/page/4.html
...have a look at the chapters left side :wink:
Very kind of you, but chinese docs is something I can't use.
Is it a chinese project?
It is, and Google Translate is your friend (and mine)
@meglio i am very disappointed that you do not know that. What have you ever read about this wonderful software? Yes, it's _chinese_ and one of the best products i have ever seen on market :1st_place_medal:
As @yellow1912 said, just translate it. I have printed this translated to paper - about 600 pages of great documentation.
By the way: they are working on a new english documentation :wink:
i am very disappointed that you do not know that
Well, I just found it out and need a quick prototype.
Would you recommend any alternative that has been there for a long time and with English documentation?
@meglio there are a few, but what impresses me with this one is the speed that they respond to inquiries and fix bugs, you don't see that happens alot with others.
This is good, but I'm concerned about the speed of prototyping if I will have to deal with translating and interpreting bad auto-translation.
I know about swoole for some weeks. I have translated for hours - but i have build a complete backend finally in 3 weeks :wink:
For fast prototyping, you can issue any question as a new issue - swoole team answers daily :)
You can test all snippets from english documentation
BTW:
There is very little documentation about daemonization of swoole and how to handle it the right way. I.e. how do we run it as a service "forever", auto-starting on reboot, and restarting on memory leaks?
This is not fast prototyping. This is handling. As swoole works as _daemon_ you can do all of that by the only way it is always done: OS-side scripts. There is no other way :wink: You can use processes to watch other processes. A process itself cannot restart itself without a parent one
@meglio maybe the examples can help you: https://github.com/swoole/swoole-src/tree/master/examples
@meglio or join the slack channel via https://www.swoole.co.uk/community
@meglio Why dont you just use supervisord to daemenize with auto-restart ability ? I didnt use swoole with it yet, but i ll give it a try.