Sha256: f819eea7f411b3211f424a8d9601d606a453122cb664a0bda7ed337aefdf2564

Contents?: true

Size: 806 Bytes

Versions: 1

Compression:

Stored size: 806 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"] || 'John'
      @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

1 entries across 1 versions & 1 rubygems

Version Path
litecable-0.4.1 examples/sinatra/chat.rb