# WebSocket Help

**URL:** <https://community.gatling.io/t/websocket-help/5017>\
**Category:** Gatling (Open-Source)\
**Created:** [May 11, 2019, 2:06am UTC](https://community.gatling.io/t/websocket-help/5017 "2019-05-11T02:06:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhawna0612](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@bhawna0612](https://community.gatling.io/u/bhawna0612)\
**Post date:** [May 11, 2019, 2:06am UTC](https://community.gatling.io/t/websocket-help/5017/1 "2019-05-11T02:06:54Z")

</div>

Hi,

I need to test our application for WebSocket. The real behaviour of our application is WebSocket connection is automatically created for the user when the user logs in the application. At this point 100 users log in before doing another action. Then at one point let’s say at 2:00 o’clock all users hit one button at the same time which sends all 100 WebSocket requests at the same time and users get the response back.

To mimic the real scenario I put the Login and WebSocket Connection request in One scenario and Subsequent actions in a different scenario. So that all users are logged in and make WebSocket connections before doing the subsequent actions concurrently. The issue is Login and WebSocket connections are Successful but for subsequent actions the error is coming that “No open WebSocket connection is fetched”

If I put the WebSocket connection and subsequent actions in a single scenario then all is going fine. What is wrong I am doing. Below is my code snippet

`  
val httpProtocol = http  
.baseUrl(“[https://qa-oh-etime-api.celayix.com](https://qa-oh-etime-api.celayix.com)”)  
.inferHtmlResources(BlackList("""._.js""", “”"._.css""", “”"._.gif""", “”"._.jpeg""", “”"._.jpg""", “”"._.ico""", “”"._.woff""", “”"._.(t|o)tf""", “”".\*.png"""), WhiteList())  
.doNotTrackHeader(“1”)  
.userAgentHeader(“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36”)  
.wsBaseUrl(“wss://qa-oh-socket-api.celayix.com”)

val headers\_10 = Map(  
“Accept” → “application/json, text/javascript, _/_; q=0.01”,  
“Content-Type” → “application/json; charset=UTF-8”,  
“Origin” → “[https://team-xpress.celayix.com](https://team-xpress.celayix.com)”)

val uri1 = “[https://dev-or-etime-api.celayix.com/CheckLogin](https://dev-or-etime-api.celayix.com/CheckLogin)”  
val uri5 = “[https://celadev.celayix.com:8453](https://celadev.celayix.com:8453)”  
val ClientID = “qaevent03”

// Method CheckLogin

def checkLogin() = {

exec(http(“CHECK\_LOGIN”)  
.post(“[https://qa-oh-etime-api.celayix.com/CheckLogin](https://qa-oh-etime-api.celayix.com/CheckLogin)”)  
.headers(headers\_10)  
.body(ElFileBody(“bodies/Check\_Login.txt”)).asJson  
.check( jsonPath( “$” ).saveAs( “RESPONSE\_DATA” ) )  
.check(bodyString.saveAs(“Response\_Body”))  
.check(substring(“Successful Login”).exists)  
.check(jsonPath("$.pcResult").is(“OK”))  
.check(regex(“pcwxSessionID”,“contextValue”:"(.+?)"").saveAs(“Session\_ID”))  
.check(regex(“pcBranchID”,“contextValue”:"(.+?)"").saveAs(“Branch\_ID”))  
.check(regex(“pcCompanyID”,“contextValue”:"(.+?)"").saveAs(“Company\_ID”))  
.check(regex(“pcEmployeeID”,“contextValue”:"(.+?)"").saveAs(“Employee\_ID”)))

.exec( session =\> {  
println( “TeamXP Login Response Body:” )  
//println( session( “RESPONSE\_DATA” ).as[String] )  
println(session (“Response\_Body”).as[String])  
println( “TeamXP SESSION\_ID:” )  
//Session\_ID = session(“Session\_ID”).as[String]  
println(session (“Session\_ID”).as[String])  
Session\_ID\_Queue.offer(session(“Session\_ID”).as[String]) // Keep session ID’s in queue  
//println(Session\_ID\_Queue)  
Employee\_ID\_Queue.offer(session(“Employee\_ID”).as[String]) // Keep employee ID’s in a queue  
//println(Employee\_ID\_Queue)  
Company\_ID = session(“Company\_ID”).as[String]  
Branch\_ID = session(“Branch\_ID”).as[String]

//println(session (“ID”).as[String])  
session  
})  
}

//Method getSelfSchedule

def getSelfSchedule() = {

exec(ws(“Get\_Self\_Schedule”)

.sendText(  
“”"{“GetSelfSchedules payload”}""")

.await(100 )  
(ws.checkTextMessage(“Get\_Self\_Schedule\_Check”)  
.check( jsonPath( “$” ).saveAs( “GET SELF SCHEDULES” ) )  
.check(jsonPath("$.payload.pcResult").is(“OK”))  
.check(regex(“pcTimeStamp”,“contextValue”:"(.+?)"").saveAs(“TimeStamp”))))

.exec( session =\> {  
//println(“GET SELF SCHEDULES:”)  
println(session(“GET SELF SCHEDULES”).as[String])  
println(“GET SELF SCHEDULES TIME STAMP:”)  
Time\_Stamp\_Queue.offer(session(“TimeStamp”).as[String])  
TimeStamp = session(“TimeStamp”).as[String]  
println(session(“TimeStamp”).as[String])  
//(session(“TimeStamp1”).as[String])  
println(session(“Session\_ID”).as[String])  
session  
})  
}

// Method updateSelfSchedule

def updateSelfSchedule() = {  
exec(ws(“Upadte Self Schedule”)  
.sendText("""{“UpdateSelfSchedules payload”}""")

.await(100 )

(ws.checkTextMessage(“Update\_Self\_Schedule\_Check”)  
.check( jsonPath( “$” ).saveAs( “UPDATE SELF SCHEDULES” ) )  
.check(jsonPath("$.payload.pcResult").is(“OK”))  
.check(substring(“The following shift has been assigned:”))))

.exec( session =\> {  
println(“UPDATE SELF SCHEDULES:”)  
println(session(“UPDATE SELF SCHEDULES”).as[String])  
session  
})  
}

val Session\_ID\_Queue = new ConcurrentLinkedQueueString  
val Employee\_ID\_Queue = new ConcurrentLinkedQueueString  
val Time\_Stamp\_Queue = new ConcurrentLinkedQueueString  
@volatile var TimeStamp = “”  
var Company\_ID = “”  
var Branch\_ID = “”  
val usersCount = 10  
//@volatile var Session\_ID = “”

//Get today date  
var date = new Date()  
val sdf = new SimpleDateFormat(“yyyy-MM-dd”)  
var startDate = sdf.format(date)  
println("Start date is: "+startDate)

// Get Future date (today+6)  
val calendar: Calendar = Calendar.getInstance  
calendar.add(Calendar.DAY\_OF\_YEAR, 5)  
var endDate = sdf.format(calendar.getTime())  
println("End date is: "+endDate)

// Scenario Check Login (Actual Script Starts here)

val EmployeeCredentialsFeeder = csv(“data/Test Employees.csv”).batch(30) // Employees Credential feeder(csv)

val TeamXP\_Login = scenario(“TXP\_Login”)

.feed(EmployeeCredentialsFeeder) // feed clientID, username and password for Login from csv file  
.exec(checkLogin()) //Execute checkLogin method  
.pause(1)

// Make Websocket Connection  
.exec(ws(“ConnectSocket”)  
.connect(“wss://qa-oh-socket-api.celayix.com/?clientId=${ClientID}&cid=${Company\_ID}&bid=${Branch\_ID}&sessionId=${Session\_ID}&eid=${Employee\_ID}”))  
.exec(ws(“Connection Check”)  
.sendText(“Ping”)  
.await(30)  
(ws.checkTextMessage(“response Check”)

.check(jsonPath("$").saveAs(“Body”)))  
)  
.exec( session =\> {  
println(“Initial Body:”)  
println(session(“Body”).as[String])  
session  
})

//create feeder for SessionID and Employee ID. poll() method will retrieve the head item from the queue and delete it  
// Here Session ID’s and Employee ID’s are polled from queue and map to Session\_ID and Employee\_ID variable

val SessionIDFeeder = Iterator.continually(Map(“Session\_ID” → Session\_ID\_Queue.poll()))  
val EmployeeIDFeeder = Iterator.continually(Map(“Employee\_ID” → Employee\_ID\_Queue.poll()))  
val TimeStampFeeder = Iterator.continually(Map(“TimeStamp” → Time\_Stamp\_Queue.poll()))

// Get and Update self Scehdule

val WS\_Connections = scenario(“Get\_Assign\_SelfSCH\_Websocket”)

//Make the variables as session variables to have them avaiable in next scenario  
.exec(_.set(“startDate”,startDate))  
.exec(_.set(“endDate”, endDate))  
.exec(_.set(“Company\_ID”, Company\_ID))  
.exec(_.set(“Branch\_ID”, Branch\_ID))

//Feed the SessionID, EmployeeID, and clientID through the feeder

.feed(SessionIDFeeder)  
.feed(EmployeeIDFeeder)  
.feed(EmployeeCredentialsFeeder)

.exec(getSelfSchedule()) // run getSelfSchedule method

.pause(3)

.exec(updateSelfSchedule()) // run getSelfSchedule method  
exec(ws(“Close WebSocket”).close)

setUp(TeamXP\_Login.inject(rampUsers(usersCount) during(10)), WS\_Connections.inject(nothingFor(15 seconds), rampUsers(usersCount) during(4)).protocols(httpProtocol))

}  
`

Below is the response I am getting

`

---

<div class="post-metadata">

**Author:** ![James\_Warr](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@James\_Warr](https://community.gatling.io/u/James_Warr)\
**Post date:** [May 11, 2019, 7:34am UTC](https://community.gatling.io/t/websocket-help/5017/2 "2019-05-11T07:34:27Z")

</div>

If you want to share data across steps you need to make them a part of the same scenario. The users executing one scenario are not the same as those executing any others - they don’t share a session context.

Any reason why you just can’t put everything in one scenario?

---

<div class="post-metadata">

**Author:** ![bhawna0612](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@bhawna0612](https://community.gatling.io/u/bhawna0612)\
**Post date:** [May 11, 2019, 12:15pm UTC](https://community.gatling.io/t/websocket-help/5017/3 "2019-05-11T12:15:31Z")

</div>

Thanks James for the reply. I can put everything in one scenario. But to mimic the behaviour of real users I wanted to put them in separate scenario because real users start to login some time ahead and waiting to hit get self schedule shifts button. Then all real users are hitting the button at same time and within a minute all 1000 shifts are gone. real users are automatically connected to webSocket at the time of login.

---

<div class="post-metadata">

**Author:** ![James\_Warr](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@James\_Warr](https://community.gatling.io/u/James_Warr)\
**Post date:** [May 11, 2019, 9:32pm UTC](https://community.gatling.io/t/websocket-help/5017/4 "2019-05-11T21:32:20Z")

</div>

Have a look at using ‘rendezVous’ to achieve this

---

<div class="post-metadata">

**Author:** ![bhawna0612](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@bhawna0612](https://community.gatling.io/u/bhawna0612)\
**Post date:** [May 12, 2019, 1:51am UTC](https://community.gatling.io/t/websocket-help/5017/5 "2019-05-12T01:51:01Z")

</div>

Thank you so much James. This is exactly what I was looking for. You made my day.

Appreciate you so much.
