Class: TurboPresenceChannel

Inherits:
ActionCable::Channel::Base
  • Object
show all
Defined in:
app/channels/turbo_presence_channel.rb

Instance Method Summary collapse

Instance Method Details

#cursor(data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'app/channels/turbo_presence_channel.rb', line 26

def cursor(data)
  x = data["x"].to_f.clamp(0.0, 1.0)
  y = data["y"].to_f.clamp(0.0, 1.0)
  TurboPresence.store.update_cursor(@room, @user_id, x: x, y: y)
  ActionCable.server.broadcast("turbo_presence:#{@room}", {
                                 type: "cursor",
                                 user_id: @user_id,
                                 x: x,
                                 y: y
                               })
end

#heartbeatObject



47
48
49
# File 'app/channels/turbo_presence_channel.rb', line 47

def heartbeat
  TurboPresence.store.touch(@room, @user_id)
end

#subscribedObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/channels/turbo_presence_channel.rb', line 4

def subscribed
  token = params[:room_token]
  model_name, record_id = TurboPresence::RoomToken.verify!(token)

  @room     = "#{model_name}:#{record_id}"
  @identity = JSON.parse(params[:identity], symbolize_names: true)
  @user_id  = @identity[:id].to_s

  stream_from "turbo_presence:#{@room}"
  TurboPresence.store.join(@room, @user_id, @identity)
  broadcast_presence
rescue TurboPresence::RoomToken::InvalidToken
  reject
end

#typing(data) ⇒ Object



38
39
40
41
42
43
44
45
# File 'app/channels/turbo_presence_channel.rb', line 38

def typing(data)
  ActionCable.server.broadcast("turbo_presence:#{@room}", {
                                 type: "typing",
                                 user_id: @user_id,
                                 name: @identity[:name],
                                 active: data["active"]
                               })
end

#unsubscribedObject



19
20
21
22
23
24
# File 'app/channels/turbo_presence_channel.rb', line 19

def unsubscribed
  return unless @room

  TurboPresence.store.leave(@room, @user_id)
  broadcast_presence
end