javascript
const { EventEmitter } = require('events');

const HIGH_WATER_MARK = 1 << 20; // 1 MiB of buffered bytes per client

class SseHub extends EventEmitter {
  constructor() {

Broadcasting Events to Clients with Server-Sent Events on Node's http Module

nodejs sse server-sent-events
by codesnips 3 tabs
python
from datetime import datetime

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

Register a Custom Flask CLI Command to Seed the Database with Faker

flask cli click
by codesnips 3 tabs
typescript
export type Validator = (value: string) => string | null;

export const required = (message = 'This field is required'): Validator => {
  return (value) => (value.trim().length === 0 ? message : null);
};

Field-Level Form Validation with a Reusable useField Hook in React

react hooks forms
by codesnips 3 tabs
java
package com.example.feed;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Cursor-Paginated Feed With Keyset Query and Spring Data Slice

spring-boot spring-data-jpa pagination
by codesnips 4 tabs
ruby
class Article < ApplicationRecord
  include PgSearch::Model

  pg_search_scope :full_text_search,
    against: { title: 'A', body: 'B' },
    using: {

Full-Text Search Endpoint in Rails with pg_search Scope and a Search Form Object

rails postgres full-text-search
by codesnips 4 tabs
javascript
import { useEffect, useRef } from 'react';

const TABBABLE = [
  'a[href]',
  'button:not([disabled])',
  'textarea:not([disabled])',

Accessible React Modal with Portal, Focus Trap, and Escape-to-Close Hook

react accessibility modal
by codesnips 3 tabs
python
import json
import secrets
import time
from typing import Optional

import redis.asyncio as redis

Sliding-Window Session Token Expiry With FastAPI Middleware and Redis

fastapi redis sessions
by codesnips 3 tabs
typescript
import { createContext } from 'react';

export type ToastVariant = 'success' | 'error' | 'info';

export interface Toast {
  id: number;

Imperative Toast Notification System with React Context and Auto-Dismiss

react toast context
by codesnips 4 tabs
php
<?php

namespace App\Security\Voter;

use App\Entity\Comment;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

Enforce Comment Ownership With a Symfony Voter and Controller Authorization Check

symfony security voter
by codesnips 3 tabs
javascript
export const required = (msg = 'This field is required') => (value) =>
  value && value.trim() !== '' ? '' : msg;

export const minLength = (n, msg) => (value) =>
  value && value.length >= n ? '' : msg || `Must be at least ${n} characters`;

Field-by-Field Signup Validation with a Reusable useForm Hook in React

react hooks forms
by codesnips 4 tabs
ruby
class User < ApplicationRecord
  has_many :posts
  has_many :comments

  scope :digest_subscribers, -> {
    where(digest_opt_in: true).where.not(confirmed_at: nil)

Weekly Digest Emails in Rails with a Cron Job and Query Scopes

rails activejob sidekiq-cron
by codesnips 4 tabs
python
from datetime import datetime

from pydantic import BaseModel


class UserV1(BaseModel):

Versioning a FastAPI API With APIRouter Mounted Under /v1 and /v2 Prefixes

fastapi api-versioning apirouter
by codesnips 4 tabs