-
Notifications
You must be signed in to change notification settings - Fork 32
/
chat.rb
41 lines (32 loc) · 823 Bytes
/
chat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
require "litecable"
require "anycable"
# Sample chat application
module Chat
class Connection < LiteCable::Connection::Base # :nodoc:
identified_by :user, :sid
def connect
self.user = cookies["user"]
self.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