How do I publish messages using GeniusesOfSymfony / WebSocketBundle?

I have used this GeniusesOfSymfony / WebSocketBundle plugin to integrate websockets in my application.

However, through the documentation, I haven't found how to insert data into a pipe after we subscribed to it, from outside the socket / connection event:

webSocket.on("socket/connect", function(session){
      session.subscribe("acme/channel", function(uri, payload){

          console.log("Received message", payload.msg);


   session.publish("acme/channel", "This is a message!");
});

      

How can I use something like session.publish from outise the socket / connection callback function?

Note that I used the "default" Theme Handler .

+3


source to share


1 answer


I just needed to create a global scope variable "sendMessage" and instantiate it using a nested function:

var sendMessage;

webSocket.on("socket/connect", function(session){
      session.subscribe("acme/channel", function(uri, payload){

          console.log("Received message", payload.msg);


   sendMessage = function(message){
        var myObject = {
         'content': message
       }
       session.publish("acme/channel", myObject);
      }
});

      



Then I just need to repeat sendMessage (message) whenever I want messages to be posted in my code.

+2


source







All Articles