Article

Send message from server to client via websocket

1. Client side

Add next code to your page in methods section:

methods: {
    init: function() {
        this.callParent(arguments);
        Terrasoft.ServerChannel.on(Terrasoft.EventName.ON_MESSAGE, this.onMessageReceived, this);
    },
    onMessageReceived: function(sender, message) {
        if (message && message.Header && message.Header.Sender === "MySenderName") {
            var result = this.Ext.decode(message.Body);
            /// TODO: your code
        }
    },
    destroy: function() {
        this.Terrasoft.ServerChannel.un(Terrasoft.EventName.ON_MESSAGE, this.onMessageReceived, this);
        this.callParent(arguments);
    }
}

2. Server side

Add usings

using Terrasoft.Configuration;
using Terrasoft.Messaging.Common;
using Newtonsoft.Json;

Send message

string senderName = "MySenderName";
// Example for message
string message = JsonConvert.SerializeObject(new {
    RecordId = Guid.NewGuid(), // your record Id
    Name = "Some name"
    // some other parameters
});
 
// For all users
MsgChannelUtilities.PostMessageToAll(senderName, message);
 
// For current user
MsgChannelUtilities.PostMessage(UserConnection, senderName, message);
 
// For specific user with sysAdminUnitId
IMsgChannel channel = MsgChannelManager.Instance.FindItemByUId(sysAdminUnitId);
if (channel != null) {
    var simpleMessage = new SimpleMessage() {
        Id = sysAdminUnitId,
        Body = message,
        Header = {
            Sender = senderName
        }
    };
    channel.PostMessage(simpleMessage);
}

 

Like 4

Like

Share

5 comments

Hi Tatiana. I know this is an old article. Follow up question - 



Can we use the in-built WebSocket to communicate back from the client to the server??

M Shrikanth,

 

Please check the response here 

https://community.creatio.com/questions/send-message-client-server-webs…

 

Best regards,

Oscar

Dear community,

 

Is it possible that a server code called from a pre-configured page is able to send message to client in another section page? Curious to understand how the client is able to recieve the messages from server. What happens if there are 2 section edit pages where the JS code is written? will the message be received in both these pages?

Shivani Lakshman,

Hi Shivani. Here is my understanding. I will let Oscar or Tatiana confirm. 



The Server to Client socket messaging is independent of which section page a user is on. Whichever client side schemas has code to receive those messages, It will be received in all those pages.



The way it works under the hood is that - There is a web socket connection which is established from the Creatio client to the server when a user logs in. This is persistent across all client pages that a user might be in. Any message triggered from the server using the code in this article will flow through that web socket. I also believe that Creatio's inbuilt notifications, Email Sync, telephony related messages are routed to the Client through this socket. 



And by 'server code called from a pre-configured page' - I presume you are referring to (for eg) a script task attached to that business process which has code to trigger the message from server to client. If it is some other way, let me know. 

Shivani Lakshman and M Shrikanth,

 

Yes, M Shrikanth, is correct in the explanation of how a web socket works. If you have two pages that should receive a message from a server, both of them can receive it through a web-socket. 

 

Regards,

Anastasiia

Show all comments