real-time

ruby
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user

ActionCable channel that streams Turbo updates safely

rails hotwire turbo
by codesnips 4 tabs
php
<?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\PrivateChannel;

Broadcasting Order Status Updates to a Private Channel with Laravel Echo

laravel broadcasting websockets
by codesnips 4 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  validates :body, presence: true, length: { maximum: 5_000 }

Declarative model broadcasts with broadcasts_to (Rails 7)

rails hotwire turbo
by codesnips 4 tabs
ruby
class TypingPresence
  EXPIRE_AFTER = 6 # seconds

  def initialize(room, redis: Redis.current)
    @room = room
    @redis = redis

Broadcast Typing Indicators in Rails Chat with ActionCable and a Redis Presence Tracker

rails actioncable websockets
by codesnips 3 tabs
ruby
class ReportsController < ApplicationController
  def create
    @report = current_user.reports.create!(
      title: report_params[:title],
      status: :pending,
      progress: 0

Turbo Streams: broadcast from a job for long operations

rails turbo hotwire
by codesnips 3 tabs
python
import asyncio
from collections import defaultdict

from fastapi import WebSocket

FastAPI WebSocket Connection Manager for Broadcasting to Room Subscribers

fastapi websockets asyncio
by codesnips 3 tabs
java
package com.example.notifications;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

Live Browser Notifications with Spring Boot SseEmitter and a Broadcaster Service

spring-boot sse server-sent-events
by codesnips 3 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  validates :body, presence: true, length: { maximum: 2_000 }

Broadcast Live Comment Updates with Turbo Streams in Rails

rails turbo hotwire
by codesnips 4 tabs
javascript
const { EventEmitter } = require('events');

class NotificationBus extends EventEmitter {
  constructor(bufferSize = 100) {
    super();
    this.setMaxListeners(0);

Server-Sent Events for Live Notifications with a Reconnectable EventSource Hook

sse server-sent-events eventsource
by codesnips 3 tabs
go
package sse

type Broker struct {
	subscribers map[chan []byte]struct{}
	subscribe   chan chan []byte
	unsubscribe chan chan []byte

Server-Sent Events in Go with http.Flusher and Context Cancellation

sse streaming http
by codesnips 3 tabs
ruby
class ImportRun < ApplicationRecord
  enum status: { pending: 0, running: 1, completed: 2, failed: 3 }

  def percent
    return 0 if total.to_i.zero?
    [(processed.to_f / total * 100).round, 100].min

Live Turbo Streams Progress Bar for a Long Rails Job

rails turbo-streams hotwire
by codesnips 4 tabs
ruby
class LikesController < ApplicationController
  before_action :set_post

  def create
    @like = current_user.likes.create!(post: @post)
    respond(liked: true)

Turbo Streams: swap a button state and counter in one response

rails hotwire turbo
by codesnips 4 tabs