flapjax get last element from eventstream

I am trying to implement a small chatservice using flapjax. I use an eventStream to get all the clients that connect to the server, and when broadcasting a message (the function on 'message') I map over this eventStream with the function that emits the message to the current client.

// Event stream yielding received clients
var clientReceiverE = receiverE();
// Event stream collecting all the clients
var clientsE = clientReceiverE.collectE([], function (client, clients) {return clients.concat([client]);});

socket.on('connection', function(client) {
    clientReceiverE.sendEvent(client);
    for (i = 0; i < chatMessages.length; i++) {
      client.emit('message', chatMessages[i]);
    }

    client.on('message', function(message) {
        chatMessages.push(message);
        //for (i = 0; i < clients.length; i++) {
        // clients[i].emit('message', message);
        //}
        mapE(clientReceiverE, function(client) {console.log(client); client.emit('message', message); return client});
    });

    client.on('nickname', function(name) {

    });
});

The registring of the clients on the eventstream succeeds with this code, but the mapE doesn't result in a loop over all this clients. Does anybody know what is wrong here?

If you are still not guessed :) I think it's because mapE doesn't produce any action itself, mapE only creates and returns another EventStream which behaves like a given source, but with modified value by means of a given function.

You should not be using mapE like that. In your code you are attempting to recreate the mapE event bindings with each client.on('message', ...).

This problem is solved using a receiverE. This function is used to translate, external event streams into flapjax EventStream 's.

// Event stream yielding received clients
var clientReceiverE = receiverE();
// Event stream collecting all the clients
var clientsE = clientReceiverE.collectE([], function (client, clients) {return clients.concat([client]);});
var clientsB = clientsE.startsWith(undefined);    //Turn the event stream into a behaviour (event + value)

var messagesE = receiverE();
messagesE.mapE(function(messagePacket){
    var clients = clientsB.valueNow();     //Grab current value of client list behaviour
    if(clients==undefined){
       return;
    }
    var from = messagePacket.client;
    for(var index in clients){
       clients[index].emit('message', messagePacket.message);
       console.log(messagePacket.message);
    }
});

socket.on('connection', function(client) {
    clientReceiverE.sendEvent(client);

    client.on('message', function(message) {
        messagesE.sendEvent({client: client, message: message});
    });
});

The difference is this. The flapjax tree is isolated from the WebSocket event code and there is no shared state between them.