Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

RPC via mysql when running under websocketd?

by schweini (Friar)
on Sep 07, 2015 at 22:59 UTC ( #1141282=perlquestion: print w/replies, xml ) Need Help??

schweini has asked for the wisdom of the Perl Monks concerning the following question:

So I'm playing around with websockets, using websocketd.

I have two users connected, and each websocketd-spawend process has an open DBI connection, but they don't really know anything about each other, apart from the fact that there is a row in a table where both session ids are stored.
Is there a way to have one process write something to the database, and that the other process immediately detects this (maybe some kind of trigger?) so that the 2nd process may process this new data, and send data to the browser which is connected to it via the websocket?

I guess the normal way would be to do this via signals and other IPC mechanisms, but if at all possible, i'd like to minimize complexity.

EDIT: i guess the sane way to go is to use SysV shared memory via one of the many available modules. But I can't find any way of monitoring shared memory in a way that if process #1 modifies it, that process #2 somehow gets notified (be it via a signal, or something like Tie::Watch
  • Comment on RPC via mysql when running under websocketd?

Replies are listed 'Best First'.
Re: RPC via mysql when running under websocketd?
by Corion (Pope) on Sep 08, 2015 at 07:44 UTC

    You could look at Postgres, which has NOTIFY. There also are many, many other methods of doing inter-process communication (IPC). Maybe using the filesystem and inotify works on your OS, or simply writing to a pipe. Using some kind of shared memory as a blackboard is also a good approach if all you care about is whether there are new events.

    You could also look at adding a message queue (server) to your setup. This brings another server into your setup. You could also use long polling HTTP ("REST") requests to notify you of a change.

    In any case, you will be looking at adding a message queue to your setup, or at least a notification queue. This will bring you additional headaches as you will need to plan on whether receiving a message and acting on it is idempotent or not. It will bring you greater flexibility because you can easily add more subscribers to your infrastructure that way.

      I ended up going the unix signal route - once a process starts, it saves its PID to the database.
      The process then goes into a while (<>) loop to wait for input (although i might want to change that to something non-blocking later on)
      When one process wants to 'speak' to another, it stores the message in the database (could use shared memory or a file for that), looks up the other process' PID, and sends it a SIGUSR1. The other process has a signal handler installed which, when triggered, looks up the data to be received in the database, and does it's thing.
      Only thing i'm worried about is race conditions between handling the signal, and waiting for and receiving input, but i think i can reduce the risk of that.
Re: RPC via mysql when running under websocketd?
by Anonymous Monk on Sep 08, 2015 at 03:30 UTC

    Is there a way to have one process write something to the database, and that the other process immediately detects this (maybe some kind of trigger?) so that the 2nd process may process this new data, and send data to the browser which is connected to it via the websocket?

    Why not polling?

      Because the whole point of using websockets for me is to reduce latency.
      And I am guessing that polling the database all the time is computationally expensive.

        Because the whole point of using websockets for me is to reduce latency. And I am guessing that polling the database all the time is computationally expensive.

        What kind of latency are you looking for?

        I doubt its expensive to query the database for messages since X ... once or three times a second ... "realtime" for humans :)

        In truth, all "events" are actually just variations of polling with different softwares doing the polling (OS...)

        See also Mojolicious: Websocket example under hypnotoad for similar discussion

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1141282]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2020-04-06 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (36 votes). Check out past polls.

    Notices?