How do I get the thread id of XMPP chat thread? - facebook-graph-api

I've implemented an XMPP Facebook chat in a browser and I wish to get the unseen messages on chat thread when I open a chat with a particular user, knowing his uid. I know this can be done by Graph API, but how do I know if this is a chat thread? Also only checking thread's 'from' field is wrong, because thread could be started by the user itself.
Thank you.

If it is part of a thread the messages will contain a element, which is the id for the actual thread.

Related

How to handle single websocket connection for all chats in Django Channels

I want to make a chat application like WhatsApp, and I want to make the backend server using Django Channels to handle all the real-time updates.
I have been exploring various sources but I could not figure out one thing about how do i manage single websocket connection (single endpoint) for each user and still receive messages from all the chats he is part of in real time. As per my current understanding, I can add channel(web socket connection corresponding to a user) to different channel groups but what if a user is part of a lot of groups(basically is eligible to receive updates from various chats)? Should I add that channel to all the groups, he can be part of as soon as the connection is established or is there any workaround like one in my mind:
Store the list of channels corresponding to each user in a database.
Make a for loop so that whenever a message is received by server, it sends message to websocket connections corresponding to each user involved to receive that message?
Any help is appreciated. Thanks in advance.
Yes, for a simple chat system, you should just add the user's channel name to the groups he's subscribed to.
However, you definitely will need to model the chat system in the database for a more complex system. Let's say you have a model Chat, ChatMember and Message. When a user connects to the websocket, he does not need to specify any chat because it is a general connection. Any message sent by the client has to specify the chat, so you can loop through the chat members and forward the message to all who are currently connected.
How do you know who is currently connected? this is the tricky part. In my architecture, I have a group for each user, sort of like an inbox. The group name is generated from the user id. Each user can have several connections, say mobile, web etc. All the connections coming from a user is added to the users group and the user's number of active connection is saved in an Inbox model. With new connections, it is incremented and decremented during disconnections.
So to know which chat members are currently online, I can just check that the user's inbox has atleast one connection. If he is online I forward the message to his ibox group, else i store the message in his inbox. Whenever a user connects, he is sent all the messages in his inbox and the inbox is cleared.
This is just an example of a way to implement it but you can also think up a custom architecture or improve on it.

c++ socket accept, list of connected clients

I have a few question about socket in c++!
First question, let's say that he writes a server for the game in which he will play 200 people at once, but accept is blocked because he already serves one client, how to deal with it?
Second question, how to download a list of all currently connected clients, so that you can then send a message to everyone?
I have a few question about socket in c++!
For future reference, please post only one question at a time. If you have multiple questions, post them separately.
let's say that he writes a server for the game in which he will play 200 people at once, but accept is blocked because he already serves one client, how to deal with it?
Use sockets in non-blocking mode, using select()/(e)poll() or other callback mechanisms to know which sockets have pending activity and when.
Otherwise, use accept() in a separate thread than other thread(s) used to service connected clients.
how to download a list of all currently connected clients, so that you can then send a message to everyone?
The server is responsible for keeping track of its connected clients. Then it can loop through that list when needed.
If a client wants to send a message to every other client, the best option is for it to send a single message to the server and ask the server to relay the message to every other client.
Otherwise, the client would have to request the list from the server, and then send a message to every other client individually.

Multithreded udp-Server vs. Non blocking calls

First questions here. I gave searched for this but haven't found any solution which fully answers my problem here. I'm using c++ and need to write a kind of usp chat (server and client) for programs to interact with one another. Well atm it works quite well.
I'm using Googles protobuf as Messages.
I've written it like that:
Server has a list of users curently logged in as well as a list of messages to process and distrubute.
One thread handles all receiving on its socket (I'm using one socket).
If command var in the message is login,
It looks through the list and checks for this combination of port and IP. If not in, the chat creates a new user entry.
If command logout, the server looks for the user in list and deletes it.
If command is message, the server looks if user is logged in and puts it on the message list.
The 2nd thread is for sending.
It waits till there is a message in the list and then cycles through all users to send this messages to their sockets except the sending one.
The server has set options on its socket to receive from any ip.
My question now is: is this the most performat solution?
I've read about select and poll. But it's always about multiple receiving sockets while I have only one.
I know the receiving thread may be idling all the time but in my environment there would be a high frequent message input.
I'm fairly new to socket programming but I think this was the most elegant solution. I was wondering if I could even create another thread which gets a list from receiving thread to process the messages.
Edit: how could I detect time outs?
I mean I could have a variable in the user list which increases or get set to 0. But what if messages won't come frequently. Maybe a server based ping message? Or maybe a flag on the message which get set to acknowledged and gets resend.
On the user side I need to first broadcast to find the server and then set port and up accordingly to the answer. How could I do that?
Because this should run autonomous. Meaning a client should detect dmthe server, login, sends its commands and detect wether it is still online.
On server side I don't know if this is so important. Possibly there might be a memory issue if there are too many things connected and non get logged off. Maybe I set a 1 h timeout to let it detect idle clients.

How to send request and receive response asynchronously to a .NET webservice by gSOAP2

I have a .NET webservice and a client program which was written by C++. The client program is using gSOAP2 to access the web service. The problem is I need to make a client request and receiving the response from server asynchronously. I search a lot by google and also read gSOAP user guide in 7.3 and 7.4 section but I still don't figure out how to do it. Please help me if you know.
Many thanks,
Tien
I don't think that gsoap means the same thing by asyncronous as you do, an asyncronous gsoap client fires of a message and then forgets about it; from reading your question my understanding is that you want to start the SOAP request/response process, go away and do something else, and then come back latter or be notified when the response has been returned.
If this is the case then I'd suggest you look at using threads to get the behaviour you want. Start a new thread to make the call, your main thread can then be notified or can check back when the call has completed. If you need data back from the call then if I was doing this I'd be tempted to write a thread that communicates via a pair of threadsafe queues. One queue to send requests into the thread and one to pass responses back out. So the main thread writes to the input queue and reads the output queue. If you search on here for C++ threadsafe queue you'll get lots more info.

django chat application using redis - how/where do I "listen" to messages after subscribing?

So I am trying to write a chat system using django (I am relatively new to real time system).
I did some research - there are lots of options (twisted, tornado etc) but for now I decided to to try and use nginx as the web server and redis's pubsub.
The chat would be between two users at a time.
Following is what I was thinking of:
On authentication all users issue a psubscribe chatctrl:*:. This essentially subscribes to a control channel to establish the initial conversation that is always needed
When a user u1 launches a chat with user u2, we
create a channel, say "chat:u1:u2" and subscribe to it.
The user u1 publishes a message to the control channel chatctrl:u1:u2: (a control message that would be listened to by u2) says effectively "do you want to chat with me on channel "chat:u1:u2"?
The user u2 should get this message, subscribes to the channel and responds as yes via another message on control channel (or on the newly established channel.
A session is established and both users can publish to the same channel and listen to it as well.
My question is:
1. Does the above make sense, first of all? If not how would you do it using redis?
2. The second question is where do I put the loop to listen to the messages. Since it would be "blocking" when there are no messages, it can not go in a view or in a model accessed by a view. Should it be in a spawned thread and if so how do I unsubscribe once the chat session is over?
Thanx!
See my answer here for an example of the system you describe.
In that code, the view spawns a Gevent greenlet that subscribes to Redis and pushes messages out to the client browser over socket.io.
The view then blocks until a message is recieved over socket.io, repeating for the duration of the chat session.
Hope that helps!