xDebug – nginx – php-fpm

A few times, I could not watch xDebug on my favorite IDE. I did check xDebug config as well as IDE, and followed up a tons of things, then it still did not work.

Then, I found out this myth: port 9000 conflict between xDebug client_port and php-fpm listen port.

It’s so frustrated that my IDE PHPStorm does not tell anything about this conflict, then I did not know anything about this.

How to solve this issue?

  • Disable php-fpm. This works for me as my server is run on Docker. The current php-fpm process is left intact due to my previous setup with the local PHP on my machine.
  • Change port in either xDebug or php-fpm, obviously.

I am using brew to manage PHP on my MacOS laptop, so this is the guide I needed to change the php-fpm port for PHP 7.4. In short, I needed:

  • Open file: /usr/local/etc/php/7.4/php-fpm.d/www.conf
  • Update 9000 to 9999 (or whatever port that does not create conflicts) in this line:
    listen =
  • Restart brew service: brew services restart php@7.4 

How to know if it’s a conflict?

Use the following command to list out the current process(es) using port 9000, which is specific for Mac but I guess you can figure it out for other OS:

sudo lsof -PiTCP -sTCP:LISTEN | grep 9000

Based on this answer.

TypeScript Learn-up

TypeScript is more and more common when writing code for modern JavaScript. In my current work project, we have started using it for a while though not fully migrated it from JavaScript to TypeScript yet. However, it’s still a good opportunity to catch this up.

I learned two following courses in February:

They’re both excellent and I am looking forward to learn the advance course from them. They use the format of giving explanations, then examples, and finally reviews after that. It’s also very good at going deep-dive and answering multiple questions “why” TypeScript makes a specific decision as well as how to avoid mistakes, overcome specific limitations, etc.

Even so, I will consider taking this Udemy course to how to fit TypeScript into a specific project, especially a large project or a non-fresh project with some existing JavaScript code. And to apply some of these knowledge, I would like to refactor my side project currency-conversion to TypeScript too.

“parameters” vs “arguments”

I have been confused and used the wrong term too, so here it is:

An important note: you’ve probably heard the terms “parameters” and “arguments” used interchangeably. Then why is this type called Parameters and not Arguments? The reason is that the two words have different meanings when we talk about programming languages in a formal way.

A parameter is part of a function’s definition, like the n in function double(n: number). An argument is a value that we pass when calling a function, like the 5 in double(5). Many programmers call both of these “arguments” in everyday conversation. Compilers and other programming language tools like TypeScript need to avoid ambiguity, so they usually distinguish between parameters and arguments.


Dependency Injection (plus Inversion of Control, and Dependency Inversion principle)

This is a note for myself to get to know more concrete details about Dependency Injection.

Dependency injection is a design pattern or at least “a set of patterns and principles; i.e. not a single pattern”. The intent behind dependency injection is to achieve separation of concerns of construction and use of objects.

This Wikipedia section is excellent in defining roles in dependency injection (their examples are great too):

  • the service objects, which contain useful functionality
  • the interfaces by which those services are known to other parts of the code
  • the client object, whose behavior depends on the services it uses
  • the injector, which constructs the services and injects them into the client

Three different types of depedency injections:

  • Constructor injection
  • Setter injection
  • Interface injection

Some thoughts from Martin Fowler:

  • Generally, Dependency Injection is better than Service Locator.
  • The choice between them is less important than the principle of separating configuration from use.
  • Prefer constructor injection over setter injection.


How to Speed Up Uploading Multiple Files via HTTP

A few months ago, I worked on a research task to find out how to improve the speed of multiple HTTP connections from one server (playing a client role) to another server (playing a server role when uploaded files are eventually saved) at the same time. In this specific issue, both servers belong to the same network. That is, we can make any change we want though allocated resource does matter.

After measurement and looking at a few layers, we found out three points to improve this connection:

  1. Implement persisent connections (keep-alive) on the server side, then the client side. If this is not implemented, after uploading a file, the related TCP connection is closed. When the next file is uploaded, a new TCP connection is constructed and this process takes quite a bit of time. It even takes more time than transferring the huge data via our network.
  2. Implement con-currency connections. That is, instead of using a single TCP connection, the client side starts a few connections at the same time, and send data via these connections. We may test and choose the optimal number to optimize the concurrency.
Continue reading “How to Speed Up Uploading Multiple Files via HTTP”

Java – My First Sip

When reading Head First Design Patterns, I need to run a few examples in Java. Plus that I also want to learn more about Java for two reasons: it’s well-known as a strongly typed programming language, and I have heard quite a lot that it’s a good language to learn OO (object-oriented) deeply.

Some notes:

Finally, these commands make Java examples of the book work:

$ cd ~/path/to/Head-First-Design-Patterns/src/headfirst/designpatterns/strategy
$ javac *.java
$ cd ~/path/to/Head-First-Design-Patterns/src
$ java headfirst.designpatterns.strategy.MiniDuckSimulator

<< Silence >>
I can't fly
I'm flying with a rocket

PHP: in_array is very slow for a huge array

Definitely not something new but this experience gave me good reason to understand how a function works under the hood.

in_array() goes through all items in a provided array and see if any existing item exists in the array. It’s not an issue if the array has only a few items. However, when it has thousands or even million items, this function is no longer usable!

Instead, a good idea is to flip values to become keys and vice versa. It’s pretty simple if all values are unique then we can just use array_flip() for this purpose and search with new keys (or old values). Good trick indeed! The reason under the hood is that array keys are implemented with Hashtable.

Otherwise, it may be a bit more complicated and we may need to do some other tricks but it can still work!

This conversation suggests using isset(). However, basically it’s still my idea above with a different way telling about that. Even though, it is still a good read!

Book: The Pragmatic Programmer

In terms of purely technical, I would say Code Complete has done better. However, this book is great at the point that it’s really practical and drives software engineers not just focus on technical parts. In addition, you need to understand the project itself, its requirements, how to communicate with other engineers as well as customers.

Still worth to give it a read! I am going to list out what caught me up most.

Continue reading “Book: The Pragmatic Programmer”

Algorithms Specialization Notes

I have finished three first algorithm courses in this specialization on Coursera. My main motivation was to fill up my foundational knowledge of computer science. Generally, I enjoyed all courses and their videos though sometimes I could only capture concepts and ideas behind all proofs. However, I am sure that I understand the motivations and steps to practice all algorithms, and translate them into real code.

Continue reading “Algorithms Specialization Notes”