realtime

ruby
# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
  def subscribed
    # Subscribe to a specific room
    room = Room.find(params[:room_id])

ActionCable for real-time WebSocket communication

ruby rails actioncable
by Sarah Mitchell 3 tabs
typescript
import { createConsumer, Cable } from '@rails/actioncable'

let cable: Cable | null = null

export function getCable(): Cable {
  if (!cable) {

WebSocket integration with Action Cable

react actioncable websockets
by Maya Patel 3 tabs
ruby
class CommentsChannel < ApplicationCable::Channel
  def subscribed
    post = find_post
    if post
      stream_for post
    else

Broadcasting New Comments to Subscribers over an ActionCable Channel

rails actioncable websockets
by codesnips 4 tabs
typescript
import { Response } from 'express';

type Client = { id: number; res: Response };

const clients = new Map<string, Set<Client>>();
let nextId = 1;

SSE endpoint for server-to-browser events

realtime sse express
by codesnips 3 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: 'User'

  # Automatically broadcast changes to all subscribers of this post
  after_create_commit -> { broadcast_append_to post, target: "comments" }

Turbo Stream broadcasts for real-time collaboration

hotwire turbo-streams actioncable
by Jordan Lee 3 tabs
typescript
import { WebSocketServer } from 'ws';
import type WebSocket from 'ws';

type ClientState = { topics: Set<string> };
const state = new WeakMap<WebSocket, ClientState>();

WebSocket server with topic subscriptions (ws)

node realtime websockets
by Mateo Rodriguez 1 tab
ruby
class NotificationsChannel < ApplicationCable::Channel
  def subscribed
    stream_for current_user
  end

  def unsubscribed

Action Cable for real-time WebSocket communication

rails actioncable websockets
by Alex Kumar 3 tabs
erb
<%= tag.div id: dom_id(comment), class: "comment", data: { controller: "comment" } do %>
  <div class="comment__body">
    <%= comment.body %>
  </div>

  <footer class="comment__meta">

Use dom_id everywhere to keep Turbo replacements deterministic

rails hotwire turbo
by codesnips 4 tabs
go
package realtime

import (
  "fmt"
  "net/http"
  "time"

Server-Sent Events (SSE) with heartbeats and client cleanup

go http sse
by Leah Thompson 1 tab