Skip to content

Instantly share code, notes, and snippets.

Using server-sent events

Why and how?

  • Documentation: https://web.dev/articles/eventsource-basics
  • Use case: broadcasting data from server to browsers
  • Benefits:
    • Easy to understand and implement (only a few lines of code)
    • No library is needed
  • Can use same HTTP(S) authentication as elsewhere in the app (which can’t be done with websockets)
@Polmonite
Polmonite / helpers.php
Last active March 31, 2023 17:21
dump and dd functions
<?php
if (!function_exists('dump')) {
function dump(...$args)
{
$message = implode(
"\n\n",
array_map(
function($value) {
return var_export($value, true);
-- Weigh rows against eachother based on different conditions,
-- ordering the results based on their given weights so that
-- more precise matches will show higher up in the results.
-- In this example, an exact match will show up at the top
-- of the results, a match at the beginning of the string
-- will show next, and a match anywhere will show last.
set @query = 'Liam';
@lyquix-owner
lyquix-owner / ip-check-allowed.php
Last active September 19, 2024 01:20
PHP function to check if it matches allowed IP addresses and subnets
<?php
// IP to check
$ip_check = $_SERVER['REMOTE_ADDR'];
// Array of allowed IPs and subnets, both IPv4 and IPv6
$ips_allowed = array(
'192.30.252.0/22'
'2620:112:3000::/44'
'192.168.16.104'
);
@tim545
tim545 / lg-jquery-app-struct.md
Last active April 1, 2025 02:54
Structuring a large jQuery application

Structuring a large jQuery application

This document assumes you are building a traditional backend-heavy application as opposed to a frontend-heavy appliction which would typically use a framework like Angular or React. The use of these frameworks make this document irrelevant, however also require a change to your application architecture and a much larger overhead in order to get content onto a page, so as a simple way to build interactive web content a simple jquery based js stack will do fine.

Directory structure

It's important you use a directory structure which is impartial to your development environment, chosen server language (Python v. Java v. C# ...), and styling framwork (Twitter Bootstrap etc). This layer of separation means you can swap out the styles or the backend with minimal changes to the Js, simple and maintainable.

Here's an example from the project root:

@morrisonlevi
morrisonlevi / Why ES6 arrow functions won't work in PHP.md
Last active February 2, 2018 09:19
Using the syntax for ES6 arrow functions wouldn't work in PHP. Here's why and how we could work around it.

In EcmaScript 2015 (ES6) the expression (x) => x * 2 means to create an anonymous function with one parameter x that will return x * 2. For example:

(x) => x * 2
// is equivalent to:
function(x) { return x * 2; }

A modified example from [documentation by Mozilla Developer Network][1] page demonstrates how they are useful:

var a = [

"Hydrogen",

The Laracasts PHPStorm theme - modified.

This is a slightly modified version of the great Laracasts PHPStorm theme. I've added some styles for Verions Control (add, modified, deleted line...) and fixed some missing things like warnings.

Download

image

Mac: Add to ~/Library/Preferences/WebIde80/colors

@nicklasos
nicklasos / parallelforeach.php
Created November 5, 2014 10:03
php parallel fork
<?php
define('PROCESSES_NUM', 6);
/**
* @param array $arr
* @param callable $func
*/
function parallelForeach(array $arr, callable $func)
{
$pid = null;
@stayallive
stayallive / install.sh
Last active May 4, 2018 19:59
Install PHP 5.6.7 on Plesk 11.5 & 12 (CentOS 6)
#!/bin/bash
# Make sure you are up to date
yum -y update && yum -y install wget
# Install EPEL repository
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# Get us a clean working directory
mkdir /php