Emit a custom event from server to particular client

741 views
Skip to first unread message

Dan Brooking

unread,
Jul 15, 2014, 2:06:42 PM7/15/14
to eas...@googlegroups.com
I see the documentation, I can do this via:

easyrtc.events.emit(event, [arg1], [arg2], [...], [callback|next]);

And I know I have the socket/easyrtcid that I can use for the client.

I just don't see or can find out how to connect those two and emit to a specific client?

I know how to do it in socket.io... is it ok to interact directly with the socket.io piece or is that advised against?

Rod Apeldoorn

unread,
Oct 2, 2014, 9:10:12 PM10/2/14
to eas...@googlegroups.com
Hey Dan,

Sorry for the late replay!

The best way to send a message from the server to a specific connection is via the "emitEasyrtcMsg" event.

Here's a quick blurb about it from the docs.

<static> onEmitEasyrtcMsg(connectionObj, msgType, msg, socketCallback, next)

Default listener for event "emitEasyrtcMsg". This event is fired when the server should emit an EasyRTC message to a client. The easyrtcid and serverTime fields will be added to the msg automatically.
Parameters:
NameTypeDescription
connectionObjObjectEasyRTC connection object. Contains methods used for identifying and managing a connection.
msgTypeStringMessage type of the message.
msgObjectMessage object which contains the full message to a client; this can include the standard msgData field.
socketCallbackFunctionSocket.io callback function which delivers a response to a socket. Expects a single parameter (msg).
nextnextCallbackA success callback of form next(err).


As for going against socket.io directly. I can see cases for and against it, so it is all up to you.

Rod Apeldoorn,
EasyRTC Server Lead,

Francesco Amici

unread,
Sep 29, 2015, 9:29:29 AM9/29/15
to EasyRTC
hi sorry, but i don't understand how send a message from server to a specific client.
can  you explain step by step what i have to do into client side and server side; i mean what function or events i have to call

thank you

James Thompson

unread,
Oct 17, 2017, 4:25:54 PM10/17/17
to EasyRTC
Like Franceso said, what is the context of sending this message?

I'm trying to do the same thing in eventListener.onAuthenticate(). I call a custom function verifyUser() that connects to MySQL and gets user/session info for valid logged in users and uses callback(null) if there are no valid users.

eventListerner.onAuthenticate() { 
if (!user) {
// WANT TO send message to specific client that acts as Audio Server
next("Error: Failed User Authentication.") } }

next(err) works as expected and the client gets a "Failed Authentication...Disconnecting" error message. But what I want to do is send a message to a specific client (my Audio Server Page) so that it has info about a failed connection. I just don't understand what the context of any of the objects in the documentation is!!! I've tried numerous things, most of which are simply undefined!

Tried:
var appObj = pub.getAppWithEasyrtcid(audioServerId, function(err) { log(err); })  // undefined
var knect = pub.getConnectionWithEasyrtcid(audioServerId, function(err) { log(err); })   // undefined

pub.util.sendSocketCallbackMsg(audioServerId, socket.socketCallback. pub.util.getErrorMsg("Text"), appObj); //socket is function arg

pub.events.emit("easyrtcMsg", knect, msg, socketCallback, function(err) { log(err); });

What is socketCallback? How do I find a valid one? Why does pub.whatever run, but just return undefined? I need a valid connection Obj and a valid socketCallback; where are they?

James Thompson

unread,
Oct 17, 2017, 8:44:53 PM10/17/17
to EasyRTC
Ok, I figured out the "undefined" problem. The functions don't return Obj, it is placed in the 2nd argument of the callback.

var conObj;
pub.getConnectionWithEasyrtcid(audioServerId, function(err, acon) {
   if (err)  console.log(err);
   conObj = acon;
});

pub.events.emit("easyrtcMsg", conObj, msgObj, socketCallback, next);

Now, if only I knew what a socketCallback was. :-)

Thanks in advance.

James Thompson

unread,
Oct 18, 2017, 6:13:29 PM10/18/17
to EasyRTC
Ok this works. The items in bold are rather difficult to glean out of the documentation, examples, or the forum.

// Audio Server Page is a special client of the Signalling Server
easyrtc.setServerListener(function(msgtype, msgdata, targeting) {
 if (msgtype === "FailedDbConnect") {
  console.log(msgdata.msgText + ": " + msgdata.user);
     easyrtc.showError(0, "Client: " + msgdata.user + " failed to Authenticate because MySQL connection failed.");
     disconnect();
      // user action is to repair MySQL and reconnect Audio Server Page to NodeJS Signalling Server; new Audio clients will
      // now be accepted; existing clients are able to reconnect automatically and auto play
 }
});

// NodeJS Signalling Server code to send easyrtcMsg to specific client that happens to serve the audio
eventListener.onAuthenticate = function(socket, easyrtcid, appName, username, credential, easyrtcAuthMessage, next) {
 // credential.session is the user.sessionId; use this to determine whether to authenticate
 const dbfail = "Db_Connect_Failed";
 var conObj, socketCb ;
 var cred = username.split('.'), msg;
 var uName = cred[0], session = cred[1], server = cred[2];
 if (server) { // the Audio Server Page tacks an extra argument on to the username to indicate it is the serverRtcId
  audioServerId = easyrtcid; // always refresh the server's rtcId when it authenticates
 }
 
 if (!credential || credential.session === "" || session !== credential.session) {
  next("Error: Invalid Session Credential supplied!");
 } else if (!uName || uName === credential.session) {
  next("Error: Invalid User supplied! Client page may have expired.");
 } else {
  try {
               verifyUser(credential, function(user) {  // connects to MySQL and validates an active user session
               if (!user) {
                   next("Error: Failed User Authentication!");
               } else if (user === dbfail) {
                msg = { "msgType" : "FailedDbConnect",
                  "msgData" : {"easyrtcid"  : audioServerId,
                  "msgText" : "User Authentication failed - Check status of MySQL.",
                  "user" : uName} };
                pub.getConnectionWithEasyrtcid(audioServerId, function(err, rcon){
                 if (err) {
                  console.log("Connect" + err);
                 } else {
                 conObj = rcon;
                 }
                 });
 
                if (conObj) {
                 conObj.socket.on("emitEasyrtcMsg", function(msg, socketCallback) {
                      socketCb = socketCallback;
                  });
                
                 // send message to specific client, telling operator of a issue with MySQL
                 pub.events.emit("emitEasyrtcMsg", conObj, msg.msgType, msg, socketCb, function(err) {
                        if (err) {pub.util.logError("["+appName+"]["+easyrtcid+"] Unhandled easyrtcMsg listener error.", err);}
                        else {
                         pub.util.log("Emitted a Msg to Audio Server Page that MySQL connection is BAD.");
                        }
                    });
                }
                   next("Error: Failed User Authentication!"); // tells Audio client that Signalling Server is not
                   // available because MySQL connection failure
               }
               else {
                 next(null);
               }
            });
     }
     catch (err) {
            next(err);
     }
  
 }
};
Reply all
Reply to author
Forward
0 new messages