websockets

python
import json
from channels.generic.websocket import AsyncWebsocketConsumer


class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):

Django channels for WebSockets and async

django python websockets
by Priya Sharma 2 tabs
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
ruby
class PresenceRegistry
  TTL = 30 # seconds a user counts as present without a heartbeat

  def initialize(room_id, redis: REDIS)
    @room_id = room_id
    @redis = redis

Action Cable Presence Tracking (Lightweight)

rails actioncable redis
by codesnips 3 tabs
ruby
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: "User"

  validates :body, presence: true

Model broadcasts: prepend on create, replace on update

rails hotwire turbo-streams
by codesnips 4 tabs
erb
<%# Subscribe every viewer of this post to its broadcasts %>
<%= turbo_stream_from @post %>

<%# Update the acting user's button to reflect the toggle %>
<%= turbo_stream.replace dom_id(@post, :like_button) do %>
  <%= render "posts/like_button", post: @post %>

Live counter updates with Turbo Streams (likes, votes)

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

  def create
    @like = @post.likes.find_or_create_by(user: current_user)

Turbo Streams: optimistic UI for likes with disable-on-submit

rails turbo stimulus
by codesnips 3 tabs