-RTCConnection = (leagueId) ->
+activeSessions = {}
+
+RTCConnection = (leagueId, element) ->
   connection = new RTCMultiConnection(leagueId)
   onMessageCallbacks = {}
   socket = io.connect('http://localhost:5555/')

  connection.connect()

clearOldSessions = (connection) ->
  for frameId of connection.sessionDescriptions
    if connection.sessionDescriptions[frameId].extra.lastMessage
      diff = new Date() - connection.sessionDescriptions[frameId].extra.lastMessage
      delete connection.sessionDescriptions[frameId] if diff > 10000
      this.fire('stream-closed', frameId)
    else
      connection.sessionDescriptions[frameId].extra.lastMessage = new Date()

Polymer
  is: 'stream-channel',

  socket.on 'message', (data) ->
    return if data.sender == connection.userid
    frameId = data.message?.sessionid
    if frameId
      element.fire("new-stream", frameId) unless activeSessions[frameId]
      activeSessions[frameId] = new Date()

    if onMessageCallbacks[data.channel]
      onMessageCallbacks[data.channel](data.message)
