ppshobi / psonic

Sonic is a super fast auto suggestion engine built by the team at crisp.chat, a customer engagement platform. its built in Rust and they officially support a javascript client, but if you want to use sonic via php, this is the library that you can look for. Completely unit tested, and adheres to modern coding standards, and offers a clean api to interact with sonic.
MIT License
130 stars 9 forks source link

Sonic Password still not working #10

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi

Still getting errors when setting a password on the channel connection. In this case:

    /**
     * @param string $collection
     * @param string $bucket
     * @param string $key
     * @param string $text
     * @return array
     */
    private function splitString(string $collection, string $bucket, string $key, string  $text): array
    {
        return str_split($text, ($this->bufferSize - (strlen($key . $collection . $bucket) + 20)));
    }
}

str_split(): The length of each segment must be greater than zero

I thought at the beginning it was a malconfiguration on the client but when I set the password back to the default one (SecretPassword) worked like a charm. Do you have an idea what it can be?

ppshobi commented 5 years ago

hey... Thanks for posting the bug. will you please post your code? So that I can test in my local, and pinpoint the issue. But probably it's because of the second argument of str_split, that calculation is returning probably a negative number because the buffer size might be too low. Still no clue about why setting the old password does fix the issue. Let me know your ideas

ghost commented 5 years ago

Hey, thank you for answering so fast. Didn't have the time to look through all the code, but I ll do as soon as I can.

I am using the php client on Laravel, setting Sonic as an external service and the connection arguments of the channel as environmental variables (host, port and password). This is a simplification of my code (actually pretty similar to the examples).

class SonicHandler {
  private $host;
  private $port;
  private $password;

  public function __construct(){
    $this->host = config('services.sonic.host'); //defined in env file
    $this->port = config('services.sonic.port'); //defined in env file
    $this->password = config('services.sonic.password'); //defined in env file
  }
  public function ingestSlug(){
    $ingest  = new Psonic\Ingest(new Psonic\Client($this->host, $this->port));
    $control = new Psonic\Control(new Psonic\Client($this->host, $this->port));

    $ingest->connect($this->password); //<- IT FAILS HERE
    $control->connect($this->password);

    $stat = $ingest->push('collection', 'bucket', 'key', 'search_text')->getStatus();

I thought at first it was not passing correctly the environmental variables but as host and port are correct, don't see the problem there.

Now, for the password. This is autogenerated key that contains special characters like for example "%gVZ8*5aONL6O#x7TEydEHX35Oo2@@". Don't think the password itself is the problem so it is passed as a literal.

To be honest, didn't try yet with overwrite the default password with the default password. I mean, passing 'SecretPassword' as an argument.

ppshobi commented 5 years ago

hmm... pretty strange issue. because I tried with really long passwords to check if it is related to the buffer size, but all of my tests are passing. So I have one more question, how are you setting the password of the sonic engine? via the .cfg file? then can you make sure the password of the sonic engine and the password you provide in the client are same?

ghost commented 5 years ago

Yes, I am using the cgf file. I am quite sure is the same password though. Not much different from the original one quoted in the default .cfg. Is working the same configuration for you then? I will take a look again tomorrow to see if I can find the problem.

ppshobi commented 5 years ago

the password you generate (ie:%gVZ8*5aONL6O#x7TEydEHX35Oo2@@) should be equal to the one in config.cfg's auth_password configuration. If you still face issue, lets do a screenshare over zoom or skype

ghost commented 5 years ago

Ok, after a couple hours looking for this problem, I found there was a bug in the way the Laravel function encodes the string, so in the end resulted in a completely different one (I guess this is due to special characters inside the password). Already solved that issue and works like a charm, thanks man.