Write a WebSocket server which sends messages to multiple clients connections at specific intervals like every 5 or 10 seconds after client connects.

283 views
Skip to first unread message

Barun Sharma

unread,
Jul 10, 2018, 1:06:55 AM7/10/18
to WebSocket++
Hi Everyone,

I have an immediate requirement. 

I need to write a WebSocket server which sends messages to multiple clients connections at specific intervals like every 5 seconds or 10 seconds after each client connect. 

For Example: 

If Client A connects at 00:05:30 -> It should receive next messages at 00:05:35, 00:05:40, 00:05:45, 00:05:50 and so.... until disconnects
If Client B connects at 00:05:33 -> It should receive next messages at 00:05:38, 00:05:43, 00:05:48 00:05:53 and so.... until disconnects
If Client C connects at 00:05:41 -> It should receive next messages at 00:05:46, 00:05:51, 00:05:56 00:06:01 and so.... until disconnects

I am unable to think any logic managing to send the same message to each client at the different time automatically considering we need to support for 100K client connections. My class design as below. 
  
#include "websocketpp/server.hpp"
#include "websocketpp/config/asio_no_tls.hpp"

class WebsocketServer {
public:
     bool init();
    void run();
    void stop();
 
    bool sendClose(string id);
    bool sendData(string id, string data);
void processMessage();
         
private:
     bool getWebsocket(const string &id, websocketpp::connection_hdl &hdl);
     
    websocketpp::server<websocketpp::config::asio> server;
    map<string, websocketpp::connection_hdl> websockets;
    LogStream ls;
    ostream os;
     
    // callbacks
    bool on_validate(websocketpp::connection_hdl hdl);
    void on_fail(websocketpp::connection_hdl hdl);
    void on_close(websocketpp::connection_hdl hdl);
};

void process_messages() { while (1) { unique_lock<mutex> lock(m_action_lock); while (m_actions.empty()) { m_action_cond.wait(lock); } action a = m_actions.front(); m_actions.pop(); lock.unlock(); if (a.type == SUBSCRIBE) { unique_lock<mutex> lock(m_connection_lock); m_connections.insert(a.hdl); } else if (a.type == UNSUBSCRIBE) { unique_lock<mutex> lock(m_connection_lock); m_connections.erase(a.hdl); } else if (a.type == MESSAGE) { unique_lock<mutex> lock(m_connection_lock); con_list::iterator it; for (it = m_connections.begin(); it != m_connections.end(); ++it) { m_server.send(*it, a.msg); } } else { // undefined. } }
}




Peter Thorson

unread,
Jul 10, 2018, 12:11:47 PM7/10/18
to Barun Sharma, WebSocket++
For a five second interval I would make five lists of connections rather than just one. When a new connection comes in I’d look at the time mod 5 to sort into one of those buckets. Then I’d probably spawn one thread per bucket that sends a message to everyone on its list and then sleeps until the next five second interval should start. Depending on the traffic volume you might also be able to use WebSocket++ Asia transport built in timers. These are simpler but won’t get the scaling benefits of multiple threads.

For very high message volumes I would just run five copies of your program and have your load balancer direct clients to backends based on the connection time as above.
--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages