I am creating a twilio video room using the rest api and setting up a statusCallback:
classroomRouter.route('/room/:id')
.get(function (req, res) {
client.video.rooms
.create({
uniqueName: req.params.id,
statusCallback: 'localhost:3000/classrooms/listen'
})
.then(function (room) {
console.log('room.sid');
console.log(room.sid);
});
});
Twilio will be sending requests to the statusCallback whenever an event happens in the room right? To the best of my knowledge, refreshing the window on which the room is rendered constitutes a participantdisconnected and participantConnected event.
But the route I setup(localhost:3000/classrooms/listen) didnt recieve any requests when i refreshed the window. Why is this?
I have a few other queries too:
Q1: Can two rooms have the same uniqueName?
(The twilio docs say that you can query a list of rooms using their uniquename. If the names are unique, how can it be a 'list' of rooms?)
Q2: If the names are indeed unique, what happens if a room already exists with uniquename I provide? Will it return the already existing room?
Q3: How can I issue this command only if the room doesnt exist?
Twilio developer evangelist here.
Twilio can't reach localhost:3000 on your machine as that is not a public address. When developing Twilio apps, we recommend you use a tool like ngrok that can tunnel into your machine and expose your application. I wrote a blog post up a while back on why I like using ngrok.
Q1: Can two rooms have the same uniqueName? (The twilio docs say that
you can query a list of rooms using their uniquename. If the names are
unique, how can it be a 'list' of rooms?)
Two rooms cannot have the same uniqueName. If you filter a list response by uniqueName then you will still get a list returned, it will just contain one room.
Q2: If the names are indeed unique, what happens if a room already
exists with uniquename I provide? Will it return the already existing
room?
If you are creating a room with a uniqueName that already exists, you will receive an error which you should handle with a catch function on your Promise.
Q3: How can I issue this command only if the room doesnt exist?
You could query the API for the room by the uniqueName first, and then decide to create it based on whether you get a response. Or you can catch the error after you try to create it and handle it that way instead.
Related
In my Node.js server, I have an array of client IDs, and I have a socket that I want to send to all connected clients except for the clients I have in that array, so I want to emit to all clients excluding some clients that i have defined by id, is that possible?
Answering my own question after some research.
One thing you could do is to add the clients you want to exclude inside of a Scoket.io room and then emit to everyone except for that room, like shown below:
io.except("room1").emit(/* ... */); // to all clients except the ones in "room1"
Note that this feature was implemented in version socket.io#4.0.0.
here is documentation for it
Hello I hope you are very well, I would like to ask you a question that I have not been able to deduce the answer to.
In a course I was seeing that to send a message to a specific user with socketIO.js the to () method is used and the id as a parameter, but I have a doubt, that id is auto generated by socketIO as far as I understand, so I would like to know How can the frontend know that id? The example that I saw in the course does it from the console and sends it directly to the method with the id that it already knows, then that example is not so real, what I would like to know in itself How is it that it performs a one-to-one chat if the id is autogenerated by the socket? I don't know if I understand.
For example, to start a conversation with another user, you can click on a button, trigger an event that makes emit, send the id of the user who writes, the event that should trigger the backend with socket, but my question is how does it taste like who send the message? How do you know the id of who is being sent to when establishing communication between 2 users for the first time? Obviously this must be sent by frontent as a parameter but also how does the frontend give this id of who will it be sent to? I don't know if you can store a fixed socket id for a user in a DB or Can you use your DB id to use with sockets? more than everything is what I can not deduce how it would be done?
I do not know if I understood with the question, more than everything is that, I do not know how it obtains or assigns the id for the target from where the message is sent and if this can be fixed and stored in db or is there any method to this.
I thank you in advance for your response, and any resources that you share with me about it or if you recommend a course with, I would greatly appreciate it.
as an example I have this method
io.on('connection', (client) => {
client.on('privateMessage', (data)=>{
const person = user.getPersona(client.id) //get this
client.broadcast.to(data.para).emit('privateMessage', createMsj( person.name, data.messages));
});
}
But where does the front-end of the person to receive the message to pass it to the method?
The front-end will not know the socket.io id of any other clients. This is where your server needs to be involved.
Each of your users presumably has some username that is displayed in the client UI and this is the name that other clients would know them by.
So, your server needs to keep a mapping between username and socket.io clientID. So, a user can send a request to your server to connect to BobS. Your server then needs to be able to look up BobS, find out if that user is currently connected and, if they are, then what is their socket.id value. That way, your server can facilitate connecting the two users.
This mapping would not typically be kept in a permanent store (such as a database) because the socket.id is a transient value and is only good for the duration of that client's socket.io connection. As such, it is more typically just kept in some sort of Javascript data structure (such as a Map object).
This is my database structure:
I want new messages to generate a notification.
How do i query this data to ensure the member is part of the conversation and then pass just the child_added of the new message.
This is what i have so far but it will return the entire conversation when a message is added.
const notificationChecker = database.ref("chats")
.orderByChild(`members/${userId}`)
.equalTo(true);
notificationChecker.on('child_changed', snapshot => {
console.log(snapshot.val());
});
Since you're listening on chats, the result of your query will be child nodes chats. There is no way to change that behavior and it is one of the many reasons why experienced Firebasers recommend working with separate top-level lists for each entity type in your app.
It seems like you want the new messages for the current user in all conversations that they are part of. That is a three step process:
Determine the current user
Find what conversations they're part of.
You'll typically do this by having a separate top-level list user-conversations, and then the UID as a key under that, and the conversation IDs as keys (with value true) under that. See https://stackoverflow.com/a/41528908/209103
Find the new messages in each conversation.
The best way to do this is to know what the last message is that the user saw in each conversation. In linear chat apps, you could model this by storing the key of the last chat message the user saw under /user-conversations/$uid/$conversationid, instead of the true we used above.
If that is not possible, you can simply load the newest message for each conversation (conversationRef.orderByKey().limitToLast(1)) and then start listening from there (conversationRef.orderByKey().startAt(keyOfLatestMessage)).
Either way, you will need to do this for each conversation that the user is part of.
I have a node.js/socket.io webapp that is currently working correctly polling an API and populating the html page with the emitted results.
My problem is that multiple people need to use this and I would like to separate their instances so that each person will only receive the results of their query.
Right now, when anyone uses the site it will return results of another user that may be also using the site.
I have tried to get around this using this method:
var clients = {};
io.sockets.on('connection', function(socket){
console.log("this is "+socket.id);
clients.id = socket.id;
})
io.sockets.socket(clients.id).emit('progress',{info:listing});
Of course this gets replaced with each new user that logs into the site so then everything that was emitted to the original user is now being emitted to the new user.
What I want to know is if there is any built-in function to get around this or if I should proceed with another persistent store.
Any help would be greatly appreciated.
Edit
By storing the socket object in the express.sessionStore instead of just in the program.
io.sockets.on(function(socket){
request.sessionStore.socket = socket;
})
The above code now works and only emits to the event originator.
This looks like it's been answered in another thread. The idea is to create an array of clients, and associated it to some type of client/user identification, like an ID or name.
Sending a message to a client via its socket.id
I create a server using nowjs. How can I serve clients grouped under an URL.
It's a wague way of putting the question. I ll give an example.
I run my server(with nowjs) in mysite.com which contains many chat rooms.
Users can join one of the chat rooms and start chatting. Real time sync happens - all handled by nowjs.
Now coming to my problem, I'm not able to differentiate between the chat rooms. Whatever chatroom user joins, since I'm using the everyone object, every user gets a message (independent of which chat room he is in).
use a group object.
I solved this issue by using
everyone.now.distributeMessage = function(message,groupname){
group = nowjs.getGroup(groupname);
group.now.receiveMessage(this.now.name, message, groupname);
}
everyone has access to that function, and by passing the groupname, only those who are members of that group get the message.