Search code examples
google-app-enginemessagechannel-api

how to communicate between two clients using channel API?


I've successfully implemented the channel api to create the connection between browser and app engine server. I want to ask what will be the way to send message from the second client to the first client. I'm not getting the actual algorithm.


Solution

  • The client_id you used to create the connection to the app engine server is what you need to send a message to another client_id. Either persist this on datastore or it is buildable by their ID but you would still need some sort of way to know what other client_id is for example:

    Create a room:

    room = models.Room(user=user_id)
    room.put()
    token = channel.create_channel(room.key.id() + user_id)
    

    Other one joins the room:

    room = models.Room.query().get()
    room.another_user = user_id
    room.put()
    token = channel.create_channel(room.key.id() + user_id)
    

    Then pass room id and token for reference on your js to send message:

    room = models.Room.get_by_id(room_id)
    send_to = room.user if room.user != user_id else room.other_user
    channel.send_message(room.key.id() + send_to, message)
    

    Note that user_id on each sample is currently connected user.