         messages: []
 
     componentDidMount: ->
-        (@props.connection).on "data", (message) =>
-            @addMessage message
+        _.forEach @props.connections, (connection) =>
+            connection.on "data", (blob) =>
+                if blob.type == "message"
+                    @addMessage blob.data
 
     render: ->
         (Dom.div null,

    render: ->
        @state.currentComponent

    onConnect: ->
        @props.addAlert type: "warning",
            (Dom.span null,
                (Dom.strong null, "Well done! "),


        @props.addAlert type: "success", "Changes saved!"

Foo = React.createClass
    displayName: "Foo"

    getInitialState: ->
        log: []

    connectionsToPeerIds: (connections) ->
        _.map connections, (connection) => connection.peer

    componentWillMount: ->
        who = getSegments()[1]
        @log "who: #{ who }"

        peer = new Peer who, key: PEER_KEY, debug: 2
        @log "new Peer()"

        connections = []

        peer.on "error", (error) =>
            alert error.type

        peer.on "open", (peerId) =>
            @log "peer.on"
            @log "peerId: #{ peerId }"

            if who == "x"
                @log "acting as #{ who }, host"

                peer.on "connection", (connection) =>
                    @log "new connection"
                    @log "peer #{ connection.peer } just connected to host #{ peerId }"

                    connections.push connection

                    peerIds = @connectionsToPeerIds connections

                    @log "connected peers: #{ peerIds }"

                    _.forEach connections, (c) =>
                        c.on "open", =>
                            @log "sending connections to #{ c.peer }"

                            c.send type: "newConnection", peerIds: peerIds

                    connection.on "data", (data) =>
                        if data.type == "message"
                            @log "MESSAGE: #{ data.message }"

            if who != "x"
                @log "acting as #{ who }, normal peer"

                connection = peer.connect "x"
                @log "connecting to x, host"

                connection.on "error", (error) =>
                    alert error

                connection.on "open", =>
                    @log "connection to #{ connection.peer } open"

                    connection.on "data", (data) =>
                        if data.type == "newConnection"
                            peerIds = data.peerIds
                            peerIds = _.filter peerIds, (peerId) -> peerId != who

                            @log "new connections without my connection: #{ peerIds }"

                            _.forEach peerIds, (peerId) =>
                                @log "connecting to #{ peerId }"

                                connection = peer.connect peerId

                                connection.on "error", (error) =>
                                    alert error.type

                                connection.on "open", =>
                                    @log "connection to #{ connection.peer } open"

                                    connections.push connection

        window.send = (message) =>
            console.log connections
            @sendMessage connections, message

    render: ->
        (Dom.pre null, _.map @state.log, (x) -> "#{ x }\n")

    log: (what) ->
        log = @state.log
        log.push what
        @setState log: log

    sendMessage: (connections, message) ->
        data =
            type: "message"
            message: message

        _.forEach connections, (c) ->
            c.send data

routes = [
    ["/chat", Chat]
    [/chat\/[a-z0-9]+/, Chat]
    ["/settings", Settings]
    [/foo\/\w+/, Foo]
]

navbarItems = [
