How to implement private messaging (one to one interactions) on sockjs-tornado?

126 views
Skip to first unread message

Sathish Panduga

unread,
Dec 25, 2015, 9:36:51 AM12/25/15
to sockjs
Dear All,

I have implemented chat feature using sockjs-tornado and could store the messages in RethinkDB.

Could you please help me on how do I establish private channel for messaging in sockjs-tornado ? (I mean Private conversation / one to one)

Below is the on_message function in my server side code -

    def on_message(self, message):
        str=message
        mg=str.split('#:#')
        sender=1 # This is the sender user id
        receiver=2 #This is the receiver user id - I need to implement session variables to have these id's so that I can use it here this way
        ts=r.expr(datetime.now(r.make_timezone('00:00')))
        connection = r.connect(host="192.x.x.x")
        r.db("djrechat").table('events').insert({"usrs":mg[0],"msg":mg[1],"tstamp":ts,"snder":sender,"rcver":receiver}).run(connection)
        log.info(message)
        self.broadcast(self.participants, '{} - {}'.format(self.stamp(),message))

In the above python code, 'msg' parameter that I get from the client contains 'userid1&userid2 #:# MESSAGE_TEXT'
Currently this is broadcasting message to all the clients who are connected.
How can I achieve one to one interaction, such that User1 and User2 only can view the messages sent between them.

May be I should have a channel id and to send message only to the two clients which will have the same channel id, but how do I implement it?

At client side, I have below javascript -

          function connect() {
            disconnect();
            conn = new SockJS('http://localhost:8080/chat', ['websocket','xhr-streaming','iframe-eventsource','iframe-htmlfile','xhr-polling','iframe-xhr-polling','jsonp-polling']);
            //log('Connecting.....');
            conn.onopen = function() {
            //  log('Connected. (' + conn.protocol + ')');
            log('Connected.');
            };

            conn.onmessage = function(e) {
              log(e.data);
            };

            conn.onclose = function() {
              log('Disconnected.');
              conn = null;
            };
          }


Am using python 3.4 - Django 1.8.4 and Rethinkdb

Please help!

-Sathish
Reply all
Reply to author
Forward
0 new messages