function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mrityunjaya VashishthaMrityunjaya Vashishtha 

How to use web sockets in salesforce ??

Hello, I want to integrate real time data using web-sockets. I have no idea where to start and what steps to follow. Please Help.
Best Answer chosen by Mrityunjaya Vashishtha
SwethaSwetha (Salesforce Developers) 
HI Mrityunjaya,
I found few related posts that should help you get started:

>>According to https://salesforce.stackexchange.com/questions/255240/create-a-websocket-server-hosted-on-salesforce-lightning , You wouldn't be able to host a WS server in Salesforce, you'd need Heroku, Amazon EB/etc, or another type of server. Salesforce doesn't have any way to host custom services like this.

>>From https://developer.salesforce.com/docs/atlas.en-us.secure_coding_guide.meta/secure_coding_guide/secure_coding_websockets.htm Example code to create a secure WebSocket:
var webSocket = new WebSocket("wss://www.salesforce.com/OAuthProtectedResource")

>>How to connect to websocket in Lightning: https://salesforce.stackexchange.com/questions/95442/how-to-connect-to-websocket-in-lightning?rq=1

>> Apex Websocket server: https://salesforce.stackexchange.com/questions/264672/apex-websocket-server
WebSocket server or GRPC(HTTP/2) are bi-directional and quite latest mid/late 2010's. So natively in Salesforce Apex you cant achieve this.
Client Component you can do that using Javascript, as its websocket API isnative to JS. You have to stick with aura/lwc component.

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Mrityunjaya,
I found few related posts that should help you get started:

>>According to https://salesforce.stackexchange.com/questions/255240/create-a-websocket-server-hosted-on-salesforce-lightning , You wouldn't be able to host a WS server in Salesforce, you'd need Heroku, Amazon EB/etc, or another type of server. Salesforce doesn't have any way to host custom services like this.

>>From https://developer.salesforce.com/docs/atlas.en-us.secure_coding_guide.meta/secure_coding_guide/secure_coding_websockets.htm Example code to create a secure WebSocket:
var webSocket = new WebSocket("wss://www.salesforce.com/OAuthProtectedResource")

>>How to connect to websocket in Lightning: https://salesforce.stackexchange.com/questions/95442/how-to-connect-to-websocket-in-lightning?rq=1

>> Apex Websocket server: https://salesforce.stackexchange.com/questions/264672/apex-websocket-server
WebSocket server or GRPC(HTTP/2) are bi-directional and quite latest mid/late 2010's. So natively in Salesforce Apex you cant achieve this.
Client Component you can do that using Javascript, as its websocket API isnative to JS. You have to stick with aura/lwc component.

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
Mrityunjaya VashishthaMrityunjaya Vashishtha
Hello Swetha, 
Thanks for the information.
Iskren Stanislavov 3Iskren Stanislavov 3
Hey Sweetha,

Could you please elaborate on this sentence: "Client Component you can do that using Javascript, as its websocket API isnative to JS. You have to stick with aura/lwc component."?
Does it mean we could have WS server in the aura/lwc?