Emitting custom event to room from server (0.10.1a)?

159 views
Skip to first unread message

Fabian Bernhard

unread,
Oct 19, 2013, 4:56:44 AM10/19/13
to eas...@googlegroups.com
Dear list,

How can I emit a custom event from my express server application to connected users within a specific room? To explain what I'd like to achieve here dummy code:

app.get("/dosomething/:roomid", function(req, res) { 
var msgobj = {};
// DUMMY
easyrtc.sendMessageToRoom(req.params.roomid, msgobj, function(err){});
});

Thank you for taking your time!

Fabian

Rod Apeldoorn

unread,
Oct 21, 2013, 6:15:00 PM10/21/13
to eas...@googlegroups.com
Hey Fabian,

It seems we need a new function! You could create your own broadcast function in v0.10.1a, but it's currently quite messy (with several levels of callbacks and arrays).

v0.10.0a and v0.10.1a were mostly about getting the client side API working well with the modular.  v0.10.2a (which should be out by the end of the week) adds a bunch on the server side to ease development there.

Note, we currently have a broadcast to room feature working from the API. Eric whipped up a quick example of that in action.

var destRoom="training_room",
    msgType="monsterAttackDrill",
    msgData= { "color":"green", "height":12, "skin":"scaley"};
    
easyrtc.sendDataWS({"targetRoom":destRoom}, msgType, msgData, function(reply) {
    if (reply.msgType === "error") {
        easyrtc.showError(reply.msgData.errorCode, reply.msgData.errorText);
    }
    else {
        ...
    }
});

Rod Apeldoorn, Priologic Software

Fabian Bernhard

unread,
Oct 22, 2013, 2:16:44 AM10/22/13
to eas...@googlegroups.com
Thank you Rod!

I have implemented the feature the way you suggest but I thought it is not so elegant if a user submits something to the server, waits for the feedback and then sends the same data to the other room members by websocket, i.e. sends the same data twice. 

Upon your response I have implemented an "emitMessage" function in easyrtc_public_obj.js, see below. I don't know if that fits the direction you want to go with easyRTC or if this is the best way to implement it. I am still new to your code and project... I'll also add it to the Github issue tracker in case someone else wants the same feature. 

Thanks once again!

Fabian

    /**
     * Emits a msg object to all connections within the same room
     *
     * @memberof    pub.appObj
     * @param       {string}    roomName
     * @param       {msg {msgType: String, msgData: Object}
     * @param       {function(Error)} callback Callback with error  
     */

    appObj
.emitMessage = function(roomName, msg, callback) {
       
       
if (!_.isFunction(callback)) {
            callback
= function(err) {};
       
}

       
if (!roomName || !appObj.getOption("roomNameRegExp").test(roomName)) {
            pub
.util.logWarning("Can not send message to room with improper name: '" + roomName +"'");
            callback
(new pub.util.ApplicationWarning("Can not send message to room with improper name: '" + roomName +"'"));
           
return;
       
}

       
if (!e.app[appName].room[roomName]) {
            pub
.util.logWarning("Can not send meesage to room which does not exists: '" + roomName +"'");
            callback
(new pub.util.ApplicationWarning("Can not send message to room which does not exists: '" + roomName +"'"));
           
return;
       
}
       
       
for (var currentEasyrtcid in e.app[appName].room[roomName].clientList) {
        appObj
.connection(currentEasyrtcid, function(err, emitToConnectionObj){
           
if (err) {
            callback
(new pub.util.ApplicationWarning("Attempt to send message to easyrtcid '" + currentEasyrtcid +"' failed"));
           
}
           
else if (!emitToConnectionObj) {
            callback
(new pub.util.ApplicationWarning("No emit connection object found for easyrtcid '" + currentEasyrtcid +"'"));
               
}
           
else {
                    pub
.eventHandler.emit("emitEasyrtcMsg", emitToConnectionObj, msg.msgType, msg, null, function(){});
               
}                    
           
});
       
}
   
};

hosein...@gmail.com

unread,
Dec 31, 2016, 2:49:39 PM12/31/16
to EasyRTC
Hi
I'm using EasyRTC v1.1.0; Have had a method to send a message from the server to a room?

I tried Fabian's method, but it didn't work!
Also, Rod's method work in client side.

Have anyone suggestion or any help for sending a message to room from EasyRTC server?

Thanks

Eric Davies

unread,
Jan 2, 2017, 12:39:03 PM1/2/17
to EasyRTC
Have you considered examining what part of Fabians method didn't work.
What I would be checking (with console.log statements in the method) is: 
    did the method get invoked with the expected arguments.
    did the method actually emit to each of the room occupants
I would also check the network console on the client to see if the messages were received but ignored by the client.




hosein...@gmail.com

unread,
Jan 2, 2017, 1:15:11 PM1/2/17
to EasyRTC
I found a solution for it:

updateListMsg = {
        msgType: 'message',
        msgData: 'salam',
        targetRoom: 'users' 
 };
easyrtc.events.defaultListeners.easyrtcMsg(connectionObj, updateListMsg, socketCallback, next);
Reply all
Reply to author
Forward
0 new messages