Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Multi-room chat

  1. #1

    Multi-room chat

    Hello,
    I was trying to generalize the chat example to obtain a multi-rooms chat. I'd like to connect clients to rooms. Each room and its clients behaves exactly as the actual chat example.

    I spent some time modifying the actual example (addressing rooms from "chat_room" parameter, replacing actual singleton room with a hashmap of rooms) but i can't see a way to associate a client (detected a metadata level) with a room (defined a data adapter level).

    Any suggestion?

  2. #2
    Power Member
    Join Date
    Feb 2008
    Location
    Siracusa
    Posts
    161
    Hi drmistral,

    could you please specify you would manage different rooms? Are they somehow statically defined in terms of number and identifier, for example in the code? How do client address the target room?

    Please let us know.

    Regards
    Gianluca

  3. #3
    Ciao Gianluca,

    I'm new in LS. I'm trying to obtain a typical facebook-like experience in an app where people can send a post in a community and other people may comment the post. So posts act as the rooms, and they are dynamically user-generated.

    My idea was to "attach" a LS-powered chat to the initial post as soon as someone starts commenting on it.
    The initial post is already managed by the app and it could be be used to address its chat-room.

    Thanks for any help.
    Last edited by drmistral; October 26th, 2016 at 12:17 AM. Reason: Better explain

  4. #4
    Power Member
    Join Date
    Feb 2008
    Location
    Siracusa
    Posts
    161
    Hi drmistral,

    to address you scenario you need to use two different kinds of subscription modes:

    1. COMMAND mode, to manage the list of dynamically user-generated posts
    2. DISTINCT MODE, to manage the comments attached to each post.

    Each client will first subscribe to an item in COMMAND mode, let's name it "POSTS", which will be used by the the Adapter to ADD new posts to the current list.

    An user interested in viewing comments attached to an existing post, should subscribe in DISTINCT mode to an item whose name is related to the post itself: for example, you might want to use a specific post identifier, so such item may be in the form of "POST_1", "POST_2", etc..
    Upon DataProvider.subscribe invocation, the DataAdapter should keep track (for example in a Map) of the item handle associated to the item name

    A comment will be generated by the user through the LighststreamerClient.sendMessage method, and then intercepted by the MetadataAdapter.notifyUserMessage method, where you'll parse the message in order to extract the post identifier and the attached comment, which will be routed to the corresponding item, by looking up the corresponding item handle from the map populated as described above.

    A new post will be generated by the user by using the same client side API. Once again, upon MetadataAdapter.notifyUserMessage, you'll need to extract the post identifier and let the Adapter add it to POSTS item, in order to update the current list of posts.

    Please have a look at our Basic Messenger Demo for an example of the COMMAND mode usage.

    Let me know whether my explanation has been clear.

    Thanks and Regards
    Gianluca

  5. #5
    Hi Gianluca,

    Ok I've just get the chat working in multi-room mode!

    My main problem was to relate a user client, through the "chat_room" in JS client, to a map
    HashMap<String, Object> room declared in ChatDataAdapter.java: how to send the chat_room value from JS client up to DataAdapter?
    Finally I've embedded it as 3rd field of message: <"CHAT", text, chat_room>, of course I've removed all controls in the demo to force singleton room and changed another couple of things and finally it worked: a client simply starts a chat with an identifier and get in a new or existing room.

    The part about COMMAND is not necessary for now because it is already managed but however useful to know.

    Thank you very much for your promptly help!

  6. #6
    Power Member
    Join Date
    Feb 2008
    Location
    Siracusa
    Posts
    161
    Hi drmistral,

    What is the purpose of the HashMap in the ChatDataAdapter?
    Is "<"CHAT", text, chat_room>" the message string sent through the sendMessage method?
    Is "CHAT" the item to which your client is subscribing to?
    Could you please provide more info about item and fields model?

    Thanks
    Gianluca

  7. #7
    Hi Gianluca, here is some details about my test to move from single room to multi-room chat. Starting point was the actual "Basic Chat Demo", client and server components downloaded from your demo page, changed parts are red.

    JS CLIENT

    First, in the client JS side I manage the name of the room through a global variable chat_room which can be set, in my case, with a reference to the post (room) id .

    [JS.1] In the Subscription management:
    Code:
    //  create the Subscription; field names will be extracted from the grid
    var chatSubscription = new Subscription("DISTINCT", chat_room, chatGrid.extractFieldList());
    [JS.2] In the submit form:
    Code:
    var mex = "CHAT|" + text + "|" + chat_room;
    ChatMetaDataAdapter

    The expected message contains a 3rd more part: the name of the room, so we address it.

    Code:
    private void handleChatMessage(String[] pieces, String message, String session) throws NotificationException {
            //extract session infos
    
            if (pieces.length != 3) {
                logger.warn("Wrong message received: " + message);
                throw new NotificationException("Wrong message received");
            }
    
            Map<String,String> sessionInfo = sessions.get(session);
            if (sessionInfo == null) {
                 logger.warn("Message received from non-existent session: " + message);
                 throw new NotificationException("Wrong message received");
            }
            //read from infos the IP and the user agent of the user
            String ua = sessionInfo.get("USER_AGENT");
            String ip =  sessionInfo.get("REMOTE_IP");
    
            //Check the message, it must be of the form "CHAT|message"
            if (pieces[0].equals("CHAT")) {
                //and send it to the feed
                if (!this.chatFeed.sendMessage(ip,ua,pieces[1], pieces[2])) {
                     logger.warn("Wrong message received: " + message);
                     throw new NotificationException("Wrong message received");
                }
            } else {
                 logger.warn("Wrong message received: " + message);
                 throw new NotificationException("Wrong message received");
            }
        }
    ChatDataAdapter.java

    Code:
    private volatile Object subscribed; 
    

    was replaced with the map
    Code:
    private volatile HashMap<String, Object> rooms = null;
    that is initialized in the init method with

    Code:
    rooms = new HashMap<String, Object>();
    The purpose of this map is to associate a chat name key to a Item handler, instead of the singleton handler (Object subscribed).

    In the subscribe method the item name is associated with the passed Handler:

    Code:
        public void subscribe(String item, Object handle, boolean arg2) throws SubscriptionException, FailureException {
    
    
    //        if (!item.equals(ITEM_NAME)) {
    //            // only one item for a unique chat room is managed
    //            throw new SubscriptionException("No such item");
    //        }
    //        assert(subscribed == null);
    //        subscribed = handle;
            
            rooms.put(item, handle);
    ...
    In the sendMessage method we get the right Item from the map.

    Code:
        public boolean sendMessage(String IP, String nick, String message, String room) {
            final Object currSubscribed = rooms.get(room);
            if (currSubscribed == null) {
                return false;
            }
        ...
    Finally in the adapter_log_conf.xml i commented out the following block

    Code:
            <!-- Optional, managed by the inherited LiteralBasedProvider. See LiteralBasedProvider javadoc. 
            <param name="item_family_1">chat_room</param>
            <param name="modes_for_item_family_1">DISTINCT</param>
            -->

    That's it. It's just a quick&dirty test, and it's also possible that I've misunderstood the usage philosophy of classes.
    Does it make sense?

    Thanks
    Last edited by drmistral; October 26th, 2016 at 07:55 PM. Reason: code formatting

  8. #8
    Power Member
    Join Date
    Feb 2008
    Location
    Siracusa
    Posts
    161
    Hi drmistral,

    thank you very much for your great effort in showing me complete details of your work. You did a very good job, in compliance of the usage philosophy.

    My only concern is related to the use of an HashMap, you should use instead a ConcurrentHashMap as concurrent threads my access such object.

    Let me know if you need any further clarification.

    Thanks and Regards,
    Gianluca

  9. #9
    Hi Gianluca, thanks a lot for your check, it was very important for me to know if I was using this stuff in the right way.
    Yes, of course ConcurrentHashMap has to be used. That was just a quick test, now I'm going to integrate LS in my project, let's see...

    Thanks for your time.

    Cheers
    Ferdinando

  10. #10
    Hi! I'm having the same issue here, I want to create some kind of chat experience, the one that is used to webcam shows online, it think it works amazing, but when I tried to do the same thing, it didn't work . I thought it was because of what you said, that you embedded it as 3rd field of message: <"CHAT", text, chat_room>. But still, it didn't work.


    Do you know why could be this happening? or what else should I try to make it work? I'm really interested to get multi-room chat with the webcam service online. Thank you for your help!

 

 

Similar Threads

  1. Chat Demo in Flash???
    By phamminh05 in forum Client SDKs
    Replies: 7
    Last Post: August 13th, 2019, 06:25 PM
  2. Replies: 1
    Last Post: April 30th, 2015, 09:51 AM
  3. chat and private message
    By ernivan in forum Client SDKs
    Replies: 1
    Last Post: March 11th, 2011, 10:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +1. The time now is 12:57 AM.