Search code examples
c++boostwebsocketsessionidwebsocket++

how to track users with websocket++ 0.3X


Is there a way to track per user data with websocket++ 0.3X?

I want to be able to identify them so to keep track of what they're looking at and what should be sent to them. Take stack as an example: while you're looking at this question, a websocket could (and I think does) keep in memory that you're looking at this question and send you the appropriate updates like votes, new comments & answers, and StackExchange updates in the upper left corner.

Also, the users need to be able to identified. Is there a session id inherent in websockets that are already hiding in websocket++? If not, how does websocket++ track users?


Solution

  • The simplest way is, as you mentioned in your answer, to use connection_hdl as a key for an associative container that stores any other data you need. WebSocket++ does have some other options for cases where that sort of lookup would be too costly.

    Since this is a common question, I've written up some more formal examples & documentation on how to do this here: http://www.zaphoyd.com/websocketpp/manual/common-patterns/storing-connection-specificsession-information.