user784756
user784756

Reputation: 2373

Private messages with gevent-socketio

I've been trying to use the python gevent-socketio library to try to send private messages by socket id, but haven't had any luck. I figured out one can send a message to the socket using the namespace via:

pkt = dict(type="event",
name="getBuddies",
args=' '.join(buddies),
endpoint=self.ns_name)
self.socket.send_packet(pkt)

and I can get and store a socket id from self.socket.sessId, but I do not know how to send a message to a specific socket id.

Upvotes: 0

Views: 555

Answers (1)

Velin Georgiev
Velin Georgiev

Reputation: 2489

Once you have your session id you can create new method in custom mxin class which should look something like this:

class CustomBroadcastMixin(object):
    ...
    def broadcast_to_socket(self, session_id, event, *args):
        """
        Broadcast to one socket only. 
        Aims to be used for wishper messages.
        """
        returnValue = False
        pkt = dict(type="event",
                   name=event,
                   args=args,
                   endpoint=self.ns_name)

        try:
            for sessid, socket in self.socket.server.sockets.iteritems():
                if unicode(session_id) == unicode(sessid):
                    socket.send_packet(pkt)
                    returnValue = True
                    break
        except:
            app.logger.exception('')

        return returnValue

than call it whenever you need it.

Cheers

Upvotes: 1

Related Questions