#TIL : View real-time logs using websocketd

I found a handy tool for making a websocket right on your shell.
It is websocketd : http://websocketd.com/

Its phisolophy is so simple :

Just read incoming text from stdin and write outgoing text to stdout. Messaging is simple.

Read its docs, I follow the ez tutorial 10 second tutorial and see how cool does it work.
Let make something cool with this, we got a UNIX tool that print out to stdout in real-time with changes of end file.

Ok cool, change the command to

1
websocketd --port=8080 tail -f logs/web_access.log

Tada ! Open the webpage that listens the websocket you made and see the magic :D You can tracking access log of website or system in real-time way.

#TIL : FTP via curl tool

Can upload an file via FTP by curl tool with handy script :

1
curl -T file_need_to_upload ftp://hostname --user user:passwd

#TIL : Eloquent Many-to-Many Relationship

To create the n-to-m relationship in Eloquent, we create a table stand between 2 tables.
Eg:

We have the db schema :

post post_tag tag
id (PK) id (PK) id (PK)
title post_id (Index) name (Unique)
content tag_id (Index)

In the model Post and Tag, we define a relation :

  • App\Model\Post.php
1
2
3
public function tags() {
return $this->belongsToMany('App\Model\Tag', 'post_tag', 'post_id', 'tag_id');
}
  • App\Model\Tag.php
1
2
3
public function posts() {
return $this->belongsToMany('App\Model\Post', 'post_tag', 'tag_id', 'post_id');
}

After that, we can use the relation to fetch the related data, like

1
2
$post = Post::firstOrFail(1);
$tags = $post->tags; // return the Collections class contains tags

or

1
2
$tag = Tag::where('name', '=', 'php')->first();
$posts_of_tag = $tag->posts; // return the Collections class contains posts

When updating or creating post, we have to sync with tags by

1
2
$tag_ids = [2, 3 ,4]; // Get tag ids from tag names
$post->tags()->sync($tag_ids);

#TIL : Index is useless when use function on indexed field

Reality, if using function on indexed field, you will broke indexing by accident.
Eg:

1
WHERE MONTH(`date`) = 11 AND YEAR(`date`) = 2015

Solution is transform the query to comparison query, like this :

1
WHERE `date` >= '2015-11-01' AND `date` < '2015-12-01'

#TIL : String Format Unicode params

1
2
unicode_thing = u"Xin chào mọi người"
a = '{}'.format(unicode_thing)

will cause the error UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 6: ordinal not in range(128)

The solution is add u prefix the pattern (it means using unicode pattern) :

1
2
unicode_thing = u"Xin chào mọi người"
a = u'{}'.format(unicode_thing)

#TIL : Debugging Chrome extension

  • To open directly the Console Dev Tools, press Cmd + Opt + J in MacOSX.
  • To debug easily the any files of extension, open the url chrome-extension://<extension-id>/<file-name> in address bar.

#TIL : Shortcut keyboard improve productivity

  • Open the preference of any Application by Cmd + ,.
  • Press Cmd + ~ to go previous App when switching App on Cmd + Tab
  • Copy screenshot to clipboard by Cmd + Ctl + Shift + 3
  • Press Option when click to notification center is putting it on “Do no disturb” mode.