Sha256: 85536a87cc78e320b2afe06d178bcd4db8ddd56a619650acebe5d631beefb75a

Contents?: true

Size: 799 Bytes

Versions: 2

Compression:

Stored size: 799 Bytes

Contents

# frozen_string_literal: true

require "litecable"

# Sample chat application
module Chat
  class Connection < LiteCable::Connection::Base # :nodoc:
    identified_by :user, :sid

    def connect
      @user = cookies["user"]
      @sid = request.params["sid"]
      # reject_unauthorized_connection unless @user
      $stdout.puts "#{@user} connected"
    end

    def disconnect
      $stdout.puts "#{@user} disconnected"
    end
  end

  class Channel < LiteCable::Channel::Base # :nodoc:
    identifier :chat

    def subscribed
      reject unless chat_id
      stream_from "chat_#{chat_id}"
    end

    def speak(data)
      LiteCable.broadcast "chat_#{chat_id}", user: user, message: data["message"], sid: sid
    end

    private

    def chat_id
      params.fetch("id")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
litecable-0.6.0 examples/sinatra/chat.rb
litecable-0.5.0 examples/sinatra/chat.rb