EasyRTC app with random call

143 views
Skip to first unread message

Roberto Rositani

unread,
Aug 15, 2015, 3:29:01 PM8/15/15
to EasyRTC
Hi to all, i would like use EasyRTC framework to create an application web.

In short this application should allow random calls between users with the same interests (e.g. sport, science, history, actuality etc)

Server stores in the database some info for each user i.e. username, pass and one (not more for simplicity) interest.

After the authentication, my idea the follow:

when a client wants to start a conversation, he should send a request to the server and the server should find a client connected with the same interest of the caller. Just find it, the server communicates to the caller easyrtcid to call.

I need some guidelines to begin.

This should be my thesis computer engineering and I would be very grateful if you gave me some advice 

Thank you.

Roberto Rositani

Eric Davies

unread,
Aug 16, 2015, 5:03:13 PM8/16/15
to EasyRTC
There are two ways you can do this, the right way, and the easy way.
The right way is storing stuff in a data base and that requires that you figure out how to query a database from node js and you figure out how to do some server programming. 

The easy way is that you say "every person has their own computer and hence it's possible to save their interests in the browsers local storage (say as a comma separated string like "baseball,football,comicbooks"), and communicate that set of interests to the server when the client connects as a room api field. Other clients in the same room (typicallly room "default") see the those values. 

Either way, finding the set of people with common interests is just a matter of doing some string operations. If you were careful to impose a strict ordering on interests, you could find the common interests between two people with a single application of the longest common subsequence (LCS) algorithm. Once you've got an implementation of LCS (should be easy to find online these days), you can probably do a basic implementation in a few hours. If you don't want to bother with LCS, a string split and some doubly nested loops, would work as well, but the LCS approach would probably intrigue your prof more.

Roberto Rositani

unread,
Aug 16, 2015, 6:31:43 PM8/16/15
to eas...@googlegroups.com
Thank you for your reply Eric.

I have to use a database and as a result I have to query it, but this is not my problem. 

Let's try to solve a step for time

The database should also store, for each user, its easyrtcid (just every client connects) (see point 2).

1. As for the login I was inspired by your example in this link: https://groups.google.com/forum/#!topic/easyrtc/2C_eUoGPPUs
After login you will be redirected to the new page (in your example "yourarein.html")

2. Now I connect to the server easyrtc and I wish the server would store the easyrtcid just got in the record of that user.
I have overrided the eventListener OnConnection but from this method I do not know how to obtain the username and I need it to know where store the new easyrtcid

Maybe I should use the session but I don't know how use it.

I hope I was clear

Thank you very much again for your attention

Eric Davies

unread,
Aug 17, 2015, 10:54:54 AM8/17/15
to EasyRTC
I think you are trying to use the wrong event. If I understand Rod's naming system properly, onConnection fires when the websocket connects, but before the authentication message (which contains the user name) is received from the client. You want to use onAuthenticated event since it gets passed a connectionObj which has a getUsername method.

Roberto Rositani

unread,
Aug 17, 2015, 7:38:00 PM8/17/15
to EasyRTC
I'd like use your example as to the login. But after login, you redirect in the page "youarein.html" with these statements (server-side): 

httpApp.post('/login', function(req, res) {
    function loginSuccess() {
        req.session.regenerate(function() {
            req.session.user = req.body.username; // can i access to the username from client js?
            res.sendfile('youarein.html', {root: 'login'});
        });
    }
    [......]

Now, from the js client-side of the page "yourarein.html", in the "init method", I connect to the easyrtc server with easyrtc.connect("Company_Chat_Line", connectSuccess, connectFailure);

Before to use that statement, I should call the setUsername(???) but here there is the problem: How can I, from this page, access to the client's username (to pass it as parameter of the method) ? Last place where I can see this information is in the  loginSuccess method (server) written above.

Thank you very much

Eric Davies

unread,
Aug 18, 2015, 12:53:46 AM8/18/15
to EasyRTC
I can't look at the old example for some reason, it seems inaccessible. However, if I get the gist of your question, you are asking, "how does the client know what his own userName is after logging in?" The answer: write some some server code to provide that information to him. I imagine you could probably embed the username in the "youarein.html" file, sending dynamic generated content instead of a static file. Or you could send it in a subsequent message once authentication had taken place (that would involve more work I suspect). However, it's your degree and I'd be surprised if you didn't come up with a few more possible ways (I'd suggest against using trained snails, I've found their latency is quite intolerable for real world applications).


Roberto Rositani

unread,
Aug 24, 2015, 11:52:04 AM8/24/15
to EasyRTC
Thank you Eric! I have solved the problem using document.cookie from the client.

Now i would solve another problem. When a client disconnects (even closing the web page), the server should modify database removing that easyrtcid. 
I have tried to use the disconnect's event from the server, writing a listener function like the follow but it seems doesn't work:

function onDisconnect(connectionObj, next) {
console.log("function onDisconnect");
var username = connectionObj.getUsername();
// here i should remove the easyrtcid of that username
easyrtc.events.emitDefault("disconnect", socket, easyrtcid, next);
}
easyrtc.events.on("disconnect", onDisconnect);


Eric Davies

unread,
Aug 24, 2015, 11:56:36 AM8/24/15
to EasyRTC
This will probably be a Rod question (he's the server guy), but when you say "it doesn't seem to work",
do you mean that your own onDisconnect function doesn't get called (it never prints  "function onDisconnect" to the server console)?

Roberto Rositani

unread,
Aug 24, 2015, 12:34:57 PM8/24/15
to EasyRTC
Yes, it never prints "function onDisconnect". I would detect, for example,  from the server when a client closes his browser. 

Rod Apeldoorn

unread,
Aug 24, 2015, 8:16:05 PM8/24/15
to EasyRTC
Hi Robeto et al,

Here is an updated 'server.js' file from the example folder. This includes events which get fired after a peer has authenticated and disconnected. I tested it using our 'Instant Messaging + Rooms' demo.

// Load required modules
var http    = require("http");              // http server core module
var express = require("express");           // web framework external module
var io      = require("socket.io");         // web socket external module
var easyrtc = require("easyrtc");           // EasyRTC external module

// Setup and configure Express http server. Expect a subfolder called "static" to be the web root.
var httpApp = express();
httpApp.use(express.static(__dirname + "/static/"));

// Start Express http server on port 8080
var webServer = http.createServer(httpApp).listen(8080);

// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer, {"log level":1});

// On Authenticated gets run after a peer has completed the authentication process
var onAuthenticated = function(connectionObj, next){
    console.log(
        "["
        + connectionObj.getEasyrtcid()
        + "]["
        + connectionObj.getUsername()
        + "] Authenticated"
    );
    next();
};

easyrtc.on("authenticated", onAuthenticated);

// On Disconnect is called after a peer has disconnected.
// Note this one should have the emitDefault run instead of next() (performs cleanup)
var onDisconnect = function(connectionObj, next){
    console.log(
        "["
        + connectionObj.getEasyrtcid()
        + "]["
        + connectionObj.getUsername()
        + "] Disconnected"
    );
    easyrtc.events.emitDefault("disconnect", connectionObj, next);
};

easyrtc.on("disconnect", onDisconnect);

// Start EasyRTC server
var rtc = easyrtc.listen(httpApp, socketServer);



In your code I didn't see anything wrong about how you called onDisconnect, though your emitDefault is sending the wrong parameter signature.

-
Rod Apeldoorn,
EasyRTC Server Lead,

Roberto Rositani

unread,
Aug 25, 2015, 5:39:49 AM8/25/15
to EasyRTC
Perfect Rod! My only wrong was that in authenticate function I didn't write next() as last statement. Sorry for my noob work! ^^'

I will write you to my next issue :D

Thank your very much!

Roberto Rositani

unread,
Aug 25, 2015, 4:13:39 PM8/25/15
to EasyRTC
Here i am with another issue! ^^'

- I have to send a message from client to server and i use follow statement (from client):
easyrtc.sendServerMessage(......);
  It works correctly because server recives the message 

- Now the server should respond to this client by sending a custom message. What is the function to use?
(from server)
function onEasyrtcMsg(connectionObj, msg, socketCallback, next) {
var username = connectionObj.getUsername();
console.log(username); // it prints correctly
// Here i should respond to that client
}

Thank you for the attention




Eric Davies

unread,
Aug 25, 2015, 5:55:09 PM8/25/15
to EasyRTC

Roberto Rositani

unread,
Aug 26, 2015, 5:29:51 AM8/26/15
to eas...@googlegroups.com
Thank you for reply. In your link i cannot open the link of the quick blurb (page not found)

I've seen your thread but I don't understand where doc says: "This event is fired when the server should emit an EasyRTC message to a client."

I have written easyrtc.events.on("emitEasyrtcMsg", onEmitEasyrtcMsg); 
and i have defined onEasyrtcMsg function but how can i run it when i want?

EDIT: I have solved with this statement connectionObj.socket.emit("easyrtcMsg", msgData, socketCallback); written from the server as last statement in the onEasyrtcMsg function.


Reply all
Reply to author
Forward
0 new messages