Search code examples
websocketphpwebsocket

How Websockets distinguish the Data Being Sent


I am reading on how to implement websockets. For now I am looking at the fancywebsockets implementation since it is easier to setup. My question is more catered on understanding websockets in general so I like to have some classification on this.

  1. Can you control the data that is being sent by the WS (I.E client receiving the data)?

  2. How do you deal with in, let's say a Private message scenario, where you also store the message in the database. If you store a text in the DB and display the message on the screen, how does WS goes about putting that change in other people browsing that page. (Does the WS listen to every changes that occurred in that PAGE?). A clear example would be in facebook, where two people browsing the same wall sees the update of the wall owner?

  3. Although a bit unrelated, but curious question. If websockets provide us a realtime update on data, why hasn't anyone created a fork version of Google Wave services. Is it because not every browser has a definite implementation of it?

Thanks.


Solution

  • About 1. Really I don't understand this question, Websockets connection is the same as TCP connection, you can modify received date before sending it back. Simple example in Bristleback Server (Server gets a preview of edited user status, then checks whether it contains illegal words and then complete preview of the status is sent back to the user:

    @ActionClass
    public class ClientNotificationUpdateAction {
    
      @Action
      public UserStatus previewEditedStatus(FacebookUser user, UserStatus status) {
        removeBadWords(status);
        return status;
      }
    }
    

    About 2. WebSockets server has information about all connected users so message can be simple sent to all connected users or additionally filtered to send this notification for example to close friends only. And here's some more code (client updates his status and this status is sent back to all his friends).

    @ActionClass
    public class ClientNotificationUpdateAction {
    
      @ObjectSender
      private ConditionObjectSender conditionObjectSender;
    
      @Action
      public void updateStatus(FacebookUser user, UserStatus status) throws Exception {
        SendCondition sendToFriendsOfUser = new FiendsOfUserCondition(user);
    
        BristleMessage message = ActionMessageFactory
          .createMessage("ClientNotificationUpdate", "updateStatus", status);
        conditionObjectSender.sendMessage(message, sendToFriendsOfUser);
      }
    }
    

    About 3. It's true that not all internet browsers implement WebSockets protocol (especially in mobile devices)