How to: Chatd (localhost:9111) and Https.

How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
Number of replies: 6

Good day, ladies and gentlemen.
Today I will tell you how to make chat using chatd. And be able to use it through https.
I warn you that all further actions are at your own risk.

So. The first one. in the chat settings specify 127.0.0.1. Yes. this tells chatd to use localhost.
You also need to set the chat server mode. I specified streaming as an update method.

The second one. You will need to configure apache for https proxy.

This is my example for apache vhost config.

Listen your_external_outside_ip:9111

<IfDefine SSL>

    <VirtualHost your_domain.somewhere:9111>

ServerName your_domain.somewhere:9111

ServerAdmin root@your_domain.somewhere

SSLEngine on

SSLProxyEngine on

SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL

SSLCertificateFile /etc/letsencrypt/live/your_domain.somewhere/fullchain.pem

SSLCertificateKeyFile /etc/letsencrypt/live/your_domain.somewhere/privkey.pem

SSLVerifyClient none

SSLProxyEngine on

    ProxyRequests off

    ProxyPreserveHost on

<Location />

    ProxyPass http://localhost:9111/

##     ProxyPassReverse http://127.0.0.1:9111/

</Location>

ErrorLog /var/log/apache2/moodle_ssl_error_log

TransferLog /var/log/apache2/moodle_ssl_access_log

CustomLog /var/log/apache2/moodle_ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

Include /etc/letsencrypt/options-ssl-apache.conf

    </VirtualHost>

</IfDefine>

The third paragraph. 

Now you need to make changes to the chat module files with your hands.
directory /home/apache/your_domain_or_somewhere/moodle/mod/chat/gui_sockets

In all files you should replace http: with https:

Next, change chatd.php:
from echo '<a target="empty" href="http://'.$CFG->chat_serverhost.':'.$CFG->chat_serverport.
to echo '<a target="empty" href="https://'.$CFG->chat_serverhost.':'.$CFG->chat_serverport.

from "; url=http://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&".
to "; url=https://$CFG->chat_serverhost:$CFG->chat_serverport/?win=users&".
4. Then, You need to run chatd in the command line:
/usr/bin/php /home/apache/your_domain_or_path_somewhere/moodle/mod/chat/chatd.php --start &

Here is an example of how the output should look.
localhost ~ # php /home/apache/your_domain_or_somewhere/moodle/mod/chat/chatd.php --start -v
Moodle chat daemon v1.0 on PHP 7.4.4

[2020-03-30 04:19:55] Setting up sockets
[2020-03-30 04:19:55] Socket opened on port 9111
[2020-03-30 04:19:55] *IMPORTANT* Started Moodle chatd on port 9111, listening socket Resource id #87
[2020-03-30 04:19:55] Polling chats to detect disconnected users

And, restart apache.

-----------------------------

And now about the problems.

Somewhere after less than a day, chatd was still running. However, when I tried to enter the chat, it exited with error:
[2020-03-30 20:14:47] Polling chats to detect disconnected users
[2020-03-30 20:16:17] Polling chats to detect disconnected users
[2020-03-30 20:16:25] * IMPORTANT * Safari identified ...
[2020-03-30 20:16:25] Incoming connection from 127.0.0.1:50254
Default exception handler: Error reading from the Debug database: MySQL server has gone away
SELECT * FROM mdl_chat_users WHERE sid =?
[array (
  0 => 'blablabla',
)]
Error code: dmlreadexception
* line 486 of /lib/dml/moodle_database.php: dml_read_exception thrown
* line 1247 of /lib/dml/mysqli_native_moodle_database.php: call to moodle_database-> query_end ()
* line 1587 of /lib/dml/moodle_database.php: call to mysqli_native_moodle_database-> get_records_sql ()
* line 1559 of /lib/dml/moodle_database.php: call to moodle_database-> get_record_sql ()
* line 1538 of /lib/dml/moodle_database.php: call to moodle_database-> get_record_select ()
* line 492 of /mod/chat/chatd.php: call to moodle_database-> get_record ()
* line 623 of /mod/chat/chatd.php: call to ChatDaemon-> promote_final ()
* line 1033 of /mod/chat/chatd.php: call to ChatDaemon-> promote_ufo ()

!!! Error reading from database !!!
Exception ignored in shutdown function tool_log \ log \ manager :: dispose: Error reading from database
google says you need to edit mysql settings.
I added the following settings to my.cnf:

[mysqld]

wait_timeout            = 3600

max_heap_table_size     = 2G

tmp_table_size          = 2G

Tomorrow I will find out if chatd will fall.

রেটিং এর গড়: -
In reply to Dmitry Fedorchenko

Re: How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
And so, chad all crashes due to mysql connection timeout, so you need to add the following changes to the function public function poll_idle_chats ($ now) {in chatd.php {

public function poll_idle_chats($now) {
global $DB;
$temp = $DB->get_record('user', ['id' => '1']);
$temp = "";

And that’s all. in theory, now chatd will constantly update the timeout connection counter. And maybe it will not fall when you connect to the chat after a long downtime.
In reply to Dmitry Fedorchenko

Re: How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
Core developers এর ছবি Peer reviewers এর ছবি Plugin developers এর ছবি

Hi Dmitry,
would you mind to file a bug report into the Tracker?
It looks like a sensitive change for those running the Moodle chat as daemon.

TIA,
Matteo

In reply to Matteo Scaramuccia

Re: How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
I see a few problems:
1) The language barrier.
2) Laziness.
3) My solution to chatd problems - dirty hacks.
4) If so far no one has logged this into a bug tracker, then few people use chatd seriously.
5) In addition, I think that those who decided to use chatd apparently already solved the problems, or refused to use it.
--------------
Although, of course, I could be wrong.
In reply to Dmitry Fedorchenko

Re: How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
!!fresh update!!
After 8h:30m its works. So idea with $temp = $DB->get_record('user', ['id' => '1']); works good. Yes, it a hack, but...its ok, its fine...
In reply to Dmitry Fedorchenko

Re: How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
Core developers এর ছবি Peer reviewers এর ছবি Plugin developers এর ছবি

Hi Dmitry,
I'll do it on behalf of you হ্যাঁ.

It sounds a good idea to ping the database to avoid connection issues, maybe using the same request or something "lighter".

Could you share here the dboptions set into your config.php e.g. to look for persistent connections?

HTH,
Matteo

In reply to Matteo Scaramuccia

Re: How to: Chatd (localhost:9111) and Https.

{$a->নাম} - {$a->তারিখ} দ্বারা
$CFG->dbtype = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'moodle';
$CFG->dbuser = 'moodleuser';
$CFG->dbpass = 'moodle2020';
$CFG->prefix = 'mdl_';
$CFG->dboptions = array (
'dbpersist' => 0,
'dbport' => '',
'dbsocket' => '',
'dbcollation' => 'utf8mb4_unicode_ci',
);