Building a WebSocket chat app (with PostgreSQL)

1,910 views
Skip to first unread message

sri

unread,
Feb 15, 2015, 6:50:10 PM2/15/15
to mojol...@googlegroups.com
So, this appears to be one of the most common WebSocket related problems we see around here. As long as you have a single process (daemon/morbo), everything is super simple. But then you want to scale up a little and use multiple processes or even servers (prefork/hypnotoad), suddenly passing around messages through some shared data structure doesn't work anymore.

In the past we would have told you to use Redis pub/sub, but a few releases ago PostgreSQL got something similar with LISTEN/NOTIFY. Only one problem, PostgreSQL does not handle huge numbers of concurrent connections very well, which is a little bit at odds with WebSockets. There is a solution though, every server process only uses one database connection for all of its message sending and receiving. And that's what the latest Mojo::Pg release (1.09) focuses on with its new pub/sub system.


The example WebSocket chat app (originally written by Joel) that is included with the release not only works out of the box, it also scales quite well.


I'm very happy with how it turned out, and hope you'll build some cool apps with it too. ;)

--
sebastian

sri

unread,
Feb 17, 2015, 1:46:45 AM2/17/15
to mojol...@googlegroups.com
Next Mojolicious release will make this even better, 0% CPU usage while managing a large number of connected WebSockets, suuuuuper smooth. :)

--
sebastian

Stefan Adams

unread,
Feb 17, 2015, 9:12:07 AM2/17/15
to mojolicious

On Tue, Feb 17, 2015 at 12:46 AM, sri <kra...@googlemail.com> wrote:
0% CPU usage while managing a large number of connected WebSockets, suuuuuper smooth. :)

What fabulous work and improvements!  I look forward to working on this!

Currently I am held back from Mojo::Pg unfortunately as I am familiar with MySQL and not Postgres.  The two do seem different enough that it will take me some time to dive into Postgres.  Any suggestions on a good starter book for Postgres?

sri

unread,
Feb 17, 2015, 12:31:13 PM2/17/15
to mojol...@googlegroups.com
Any suggestions on a good starter book for Postgres?

Some folks like books that are heavy on theory, but i'm more the pragmatic type and like examples. These books worked for me, when i started with PostgreSQL again last year.


And of course the official documentation is excellent too.


--
sebastian

Stefan Adams

unread,
Feb 17, 2015, 12:39:23 PM2/17/15
to mojolicious

On Tue, Feb 17, 2015 at 11:31 AM, sri <kra...@googlemail.com> wrote:
Some folks like books that are heavy on theory, but i'm more the pragmatic type and like examples. These books worked for me, when i started with PostgreSQL again last year.


Thank you!  I appreciate you taking the time to offer your suggestions.  Also preferring examples (something I love about the Mojo docs), I look forward to reading PostgreSQL: Up and Running and I have read the SQL Cookbook a couple of times now.  That book was remarkable!

sri

unread,
Feb 17, 2015, 12:47:04 PM2/17/15
to mojol...@googlegroups.com
These books worked for me, when i started with PostgreSQL again last year.

There are also a few things i wish someone had told me about when i started. :)


You can actually search for an array of values with "foo = any (?)"!


You can have optional conditions with "(state = $1 or $1 is null)" and the query planner will optimize it out if you pass an undef value.

--
sebastian

sri

unread,
Feb 18, 2015, 1:23:11 AM2/18/15
to mojol...@googlegroups.com
Next Mojolicious release will make this even better, 0% CPU usage while managing a large number of connected WebSockets, suuuuuper smooth. :)

And released! ;)

--
sebastian 

Paul Williams

unread,
Feb 18, 2015, 7:36:08 AM2/18/15
to mojol...@googlegroups.com
This is a great example of using the LISTEN/NOTIFY feature in postgresql. The work you've done to make it so succinct quite is amazing.

I will be taking advantage of this feature to allow people to listen to a websocket to listen for completed Minion jobs. Postgres triggers are also grrreeat.


-Paul

sri

unread,
Feb 21, 2015, 4:23:18 PM2/21/15
to mojol...@googlegroups.com
Postgres triggers are also grrreeat.

Indeed they are. I love that you can use triggers to send notifications.


--
sebastian 

sri

unread,
Mar 1, 2015, 11:54:35 AM3/1/15
to mojol...@googlegroups.com
Similar chat app now also ships with Mojo::Redis2.


--
sebastian

Yogesh Chandra

unread,
Aug 5, 2015, 7:48:27 AM8/5/15
to Mojolicious
0% CPU usage - curious, how does it manage that?
Reply all
Reply to author
Forward
0 new messages