Search code examples
c++websocketbeast

Boost::Beast Websocket Bidirection Stream (C++)


I'm looking into using the Boost::Beast websocket library to create an asynchronous bidirectional pipe to pass data between a server and a client. I leveraged some code from the async example (I can post some at a later time if necessary, don't have access to it now). I currently have a class which creates several threads running a SocketListener. When a client connects, it creates a Session shared_ptr to do the async read and write functions. The problem is, this session object will only write out when the client has sent me a message. I'm looking for an implementation that allows my server to write on demand to all the clients connected to it and also listen for incoming data from those connections.

Is this possible? Am I using the wrong technique for this? The other way I though this may be achievable is to have an incoming websocket and and outgoing websocket. Incoming would allow a client to drop configurations for the server and outgoing would just monitor a message queue and do a async write if a message is available.

Thanks!


Solution

  • Is this possible?

    Yes

    Am I using the wrong technique for this?

    No

    The other way I though this may be achievable is to have an incoming websocket and and outgoing websocket, and No respectively.

    That is not necessary, a websocket stream is full-duplex. You can read and write at the same time.

    outgoing would just monitor a message queue and do a async write if a message is available.

    This is the correct approach, but you can do that in the same Session object that also handles the reads.

    Here's an example that reads continuously and can also write full-duplex: https://github.com/vinniefalco/CppCon2018