Event object of "streamCreated" returns wrong user

Hello! I´m struggling with an issue: When I print out the event object of “streamCreated” event.stream.connection.data contains the info of the user who logged in last. I need the data of the user who joined the session last. The problem occurs when a user leaves a session, remains logged in and rejoins. When the user leaves, logs out, logs in and joins again it´s good.

My environment is a NodeJS server and a vanilla JS frontend. I am using the server.js almost unchanged from the tutorial.

Any help would be appreciated.

I’m sorry but I don’t understand your issue.

Can you describe what is the behaviour you want and what is the behavior you get? If it seems a bug, can be reproduced with an official tutorial?

Regards

Hi Micael. Let´s say I have three users A, B and C in a session. User C logged in last. Now User B leaves the session but does not log out. When user B rejoins the session the event of the “streamCreated” handler returns the data of user C who logged in last instead of user B who just rejoined the session.

session.on('streamCreated', event => {
		console.log(event.stream.connection.data);  // Should contain data of user who joined last
		var subscriber = session.subscribe(event.stream, 'video-container');
		subscriber.on('videoElementCreated', event => {
			appendUserData(event.element, subscriber.stream.connection);
		});
	});

Hope that makes it understandable.

And yes, it can be reproduced with the tutorial openvidu-js-node. (Just add a third user publisher3 in server.js mock db)

Hi @MartinW,

the streamCreated event is fired when a stream is correctly created. If you join to a created session with participants into it, you will receive as many streamCreated events as there are streams. This is:

  1. You have A, B and C uses connected.
  2. User B leave the session and rejoin
  3. A and C should receive the streamCreated event of user B
  4. B should receive the streamCreated event of Users A and C.

Knowing that, do you mean that user B only receive the streamCreated event of user C?

Hello @CSantosM,

Did you mean “A and C should receive the streamCreated event of user B”?

Nope, that´s not what I meant. I receive as many streams as there are streams. It is exactly like you described it. The only problem I noticed, is, that event.stream.connection.data of user A and C contains data of C after rejoin of B. But it should contain data of user B. And that is the case since C was the last user that has logged in.

It probably isn´t a bug in openvidu itself, but in the Node server.js file. There I found the following line (near 92 in the login function):

req.session.loggedUser = user;

So loggedUser always contains the last one that logged in. Can this be improved?

You will have problems if you log in into the same application using tabs of the same browser, of course.

To test applicaitons with user management you must always use a normal window and an incognito window, or you will confuse the application.

Hi Pablo,

I have a normal instance of FF for user A, an incognite instance for user B and a second incognito instance of FF for user C.

OK. I found something myself. What I obviously did wrong was leaving the second parameter of session.connect empty.

session.connect(token)

So I added an object with the client data again:

session.connect(token, { clientData: userObj })

What confused me was the usage of client and server data in appendUserData():

function appendUserData(videoElement, connection) {
var clientData;
var serverData;
var nodeId;
if (connection.nickName) { // Appending local video data
	clientData = connection.nickName;
	serverData = connection.userName;
	nodeId = 'main-videodata';
} else {
	clientData = JSON.parse(connection.data.split('%/%')[0]).clientData;
	serverData = JSON.parse(connection.data.split('%/%')[1]).serverData;
	nodeId = connection.connectionId;
}
var dataNode = document.createElement('div');
dataNode.className = "data-node";
dataNode.id = "data-" + nodeId;
dataNode.innerHTML = "<p class='nickName'>" + clientData + "</p><p class='userName'>" + serverData + "</p>";
videoElement.parentNode.insertBefore(dataNode, videoElement.nextSibling);
addClickListener(videoElement, clientData, serverData);

}

This line …

serverData = JSON.parse(connection.data.split('%/%')[1]).serverData;

… always contains the last data of the Node server (the last user that logged in). Therefor to get the correct username clientData must be used.

Just to round the case up. Thanks for helping!