 
 	getInstanceByRoomId: (roomId) ->
 		if not @instancesByRoomId[roomId]?
-			@instancesByRoomId[roomId] = new WebRTCClass Meteor.user().username, roomId
+			@instancesByRoomId[roomId] = new WebRTCClass Meteor.userId(), roomId
 
 		return @instancesByRoomId[roomId]
 
 
+Meteor.startup ->
+	RocketChat.Notifications.onUser 'webrtc', (type, data) =>
+		if not data.room? then return
+
+		webrtc = WebRTC.getInstanceByRoomId(data.room)
+
+		webrtc.transport.onUserStream type, data

			@log 'WebRTCTransportClass - onRoom', type, data

			switch type
				when 'call'
					if @callbacks['onRemoteCall']?.length > 0
						fn(data) for fn in @callbacks['onRemoteCall']

				when 'join'
					if @callbacks['onRemoteJoin']?.length > 0
						fn(data) for fn in @callbacks['onRemoteJoin']

				when 'candidate'
					if @callbacks['onRemoteCandidate']?.length > 0
						fn(data) for fn in @callbacks['onRemoteCandidate']

				when 'description'
					if @callbacks['onRemoteDescription']?.length > 0
						fn(data) for fn in @callbacks['onRemoteDescription']

				when 'status'
					if @callbacks['onRemoteStatus']?.length > 0
						fn(data) for fn in @callbacks['onRemoteStatus']

	startCall: (media) ->
		@log 'WebRTCTransportClass - startCall', @webrtcInstance.room, @webrtcInstance.selfId
		RocketChat.Notifications.notifyRoom @webrtcInstance.room, 'webrtc', 'call',
			from: @webrtcInstance.selfId
			audio: media.audio
			video: media.video

	joinCall: (media) ->
		@log 'WebRTCTransportClass - joinCall', @webrtcInstance.room, @webrtcInstance.selfId
		RocketChat.Notifications.notifyRoom @webrtcInstance.room, 'webrtc', 'join',
			from: @webrtcInstance.selfId
			audio: media.audio
			video: media.video

	sendCandidate: (data) ->
		@log 'WebRTCTransportClass - sendCandidate', data, @webrtcInstance.room
		RocketChat.Notifications.notifyRoom @webrtcInstance.room, 'webrtc', 'candidate', data

	sendDescription: (data) ->
		@log 'WebRTCTransportClass - sendDescription', data, @webrtcInstance.room
		RocketChat.Notifications.notifyRoom @webrtcInstance.room, 'webrtc', 'description', data

	sendStatus: (data) ->
		@log 'WebRTCTransportClass - sendStatus', data, @webrtcInstance.room
		RocketChat.Notifications.notifyRoom @webrtcInstance.room, 'webrtc', 'status', data

	onRemoteCall: (fn) ->

			video {Boolean}
	###
	onRemoteCall: (data) ->
		fromUsername = Meteor.users.findOne(data.from)?.username
		if data.video and data.audio
			title = "Audio and video call from #{fromUsername}"
		else if data.video
			title = "Video call from #{fromUsername}"
		else if data.audio
			title = "Audio call from #{fromUsername}"

		swal
			title: title
