#Confused while using WebSockets

5 messages · Page 1 of 1 (latest)

lyric canyon
#

Hey
I am inexperienced in WebSockets
In the index.js
When I say

socket.on('message', data=>{
    /*Stuff with data*/
})```
This is going to run every time I receive a message from a connected client right?
Lets say I want the client to report its name, which it would send through the socket.
Later I might want the client to send something else with which I would want to do something different. I do not understand how will I differentiate between such data when all I can define is a single function. 
Say, when i recieve the name
i have 
```js
socket.on('message', data=>{
    clientObject.name = data;
})```

But then how should I later use this to lets say, take age? when Im already using this for name? If im somehow forced to use 2 separate send requests from the client, I dont know how will I process them separately in the index.js
hazy folio
lyric canyon
#

would this be an ideal way of dealing with this?

hazy folio
#

yes. the messages you send would be objects with a "type" so you know how to handle it