29.01.20

Fixing Server Side Events and Connection Aborted Issues

- Uncategorized -

Server Side Events offer an easy way to push data to clients. It’s much easier to implement than websockets or another technology.

IF you stumble on the problem of connections not getting aborted, when clients closed the event resource, or if you see the data coming in chunks, you need to tweak your PHP server, or at least fix that behaviour by setting additional header.

On Nginx, with gzip compression enabled, you need at least these:

header("Content-Type: text/event-stream");
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');

And you need to output a ping line of data, if you’re not outputting any real data, so that the connection doesn’t stay idle for too long. Then it will be closed by the server.

echo "event: ping\n",'data: {"time": "' . date(DATE_ISO8601) . '"}', "\n\n";

If you’re server still has a lot of running fpm processes, reload the php-service to get rid of them:

systemctl reload php7.4-fpm.service

Reload the Nginx server won’t help.

systemctl reload nginx.service

05.12.19

Setting default values for requests the easy way

- PHP, Uncategorized -

PHP 7 introduced the Null Coalesce Operator, and this is a very neat addition to PHP in practice. It will let you write code that is much easier to read and much more concise.

You don’t have to wrestle anymore with isset() and ternary operators.

Just write this:

$offset = $_GET['offset'] ?? 0;

It there is now offset parameter in the GET request, $offset will be assigned the default value 0.

This is much like the way you can do that in Python or Javascript.

Before 7 you needed to write this in PHP:

if (isset($request->input['offset'])) {
$offset = $request->input['offset'];
} else {
$offset = 0;
}

08.11.19

JSON Encoding of number problem

- Uncategorized -

You can get quite surprising results if you try to json_encode something like json_encode($n[“number”]=3.53).

echo json_encode($n["number"]=3.53);
3.5299999999999998

Seems there is a bug in PHP, that parse floats with wrong precision.

Currently json_encode() uses EG(precision) which is set to 14.

The solution is to cast the number to string:

echo json_encode($n["number"]=(string) 3.53);
"3.53"

Another solution is to set the precision explicitly:

ini_set( 'serialize_precision', -1 );
echo json_encode($n["number"]=3.53);
3.53

Weird bug, that’s causing quite some headscratching.

You could also set it in php.ini:

serialize_precision = -1

23.02.17

How to format JSON in VIM/GVIM

- Tutorial -

The strength and beauty of VIM is the simple interaction with lots of external programs.

To format any JSON file in VIM/GVIM:

:% !jq .

% means the file you’re working on, and `!jq` is run the external command `jq`

That’s all.

You need to have `jq` installed:
sudo apt install jq
whatis jq
jq (1) - Command-line JSON processor

03.02.17

The ease and beauty of VIM

- Uncategorized -

Never thought that I would write a post with the ease of VIM in the title. But honestly I tried a lot of editors still coming back to VIM. VIM is a lot more hackable then Atom.

Probably because I like bash, python and javascript, while Atom is javascript hackable only.

VIM is not easy at first sight, and certainly not easy at first touch. Counterintuitive to the noob, all the difference modes VIM can be in, you somehow love it after a while.

Try the VIM editor

If you’re interested just try it out.
apt install vim

Run vimtutor after you installed VIM. I surely recommend the great VIM casts, of Drew Neil. Excellent learning stuff. Best to watch after you installed VIM and tried the VIMTutor.

Back to the title, the ease of VIM for productivity. Sure you need to extend it and use plugins. UltiSnips is one of my favorite.

Use snippets with UltiSnips

A good setup of snippets are a real time saver. There are pre-installed snippets, but you need to setup your own snippets, because they reflect how you think, and they should suit into your workflow.

For testing sites on Apache, or protecting a WordPress login from, you often use access control,  to restrict access to portions of your site based on the host address of your visitors.

You create a Deny Allow directive  for that in .htaccess.

order deny,allow
deny from all
allow from [your-ip]

Create a deny allow snippet

To speed up inserting earlier code in any .htaccess file your editing in VIM/GVIM, create a apache.snippets file in your UltiSnips directory (~/.vim/UltiSnips by default), and add this to the file:

snippet deny "Deny allow snippet" b
order deny,allow
deny from all
allow from `curl ipinfo.io/ip`
endsnippet

Done, every time you type `deny` hit tab and the snippet is inserted with the ip-address of the computer you’re working on. Through  `bash interpolation`, the snippets insert the output of the command `curl ipinfo.io/ip` which is the linux way of getting your ip-adress. Compare that to Microsofts Windows way. Never say again that Linux is not easy.

So, easy after all, isn’t it? That’s why I call VIM easy. Of course it isn’t actually, but there is beauty in the way it works, and the way you can make it work, fast en easy.

Categories
-->
Archives
Links