otp


Online Users Storing Elixir


I am working on one chatroom [all to all] application in Elixir using OTP Genserver and getting messages from js client as user gets registered with their names as first phase. Now, just bit not sure what would be the best approach to store these names at my elixir server somehow and send regular updates to client with list of users online or database storage. Please suggest the best approach.
I agree with bitwalker that ETS is a good fit.
Here's a short summary of what I did in production. It wasn't a chat server, but a server push with a couple of thousand of users connecting via long polling. Pushed data was divided in some 50 categories, and users were able to choose which ones they want. At peak times the server pushed new messages each 2 secs, and processed > 2000 reqs/sec.
Essentially, I kept a gen_server for each user, where I held pending messages and user's configuration (basically a list of selected channels). This was beneficial with long polling, since user's data is decoupled from the user's request, so the data remains while requests are transient. However, I think this approach is also good for permanent connections, such as websockets, since there might still be occasional disconnections, and keeping a more stable user's data gives you a chance of resuming after reconnect.
Obviously, when a request arrives, you need to find the user specific process, and for this, ETS is a good fit, since you don't have a single process bottleneck. Instead of manually working with ETS, I'd recommend using gproc in conjunction with via tuples. Basically, when starting a user's gen_server, you can provide name: {:via, :gproc, {:n, :l, key}} where key is some custom key (arbitrary term) you make based on your internal user's id(:n and :l indicate a unique name on the local node). You can then use that same via tuple when issuing calls/casts, and gen_server will use gproc to find the corresponding process.
Finally, you need to have some timeout/disconnect logic to cleanup user processes. In my case, I simply terminated a user's process if there was no activity from the web layer (no end-user came for data in some time). Gproc will automatically remove entries for terminated process from its internal ETS table. It's probably best to supervise user processes under a temporary strategy.
I realize all of this is still a bit vague, but I hope it makes some sense. Keep in mind that this is not the ultimate pattern (there's no such thing of course), but I think it's a reasonable first attempt.
You may also want to take a look at Phoenix web framework that has an interesting pub-sub facility in form ofTopics. I didn't try this out myself yet, but it seems interesting, and may even simplify some of the stuff I discussed above, or at least help for pushing notifications from chatroom to all users.
Sounds like a good use case for ETS.
A simpler approach might be to use an Agent to store the online users information, but it depends quite a lot on what you need from the storage mechanism you choose.

Related Links

How to build a release archive with rebar3 together with lager
Open trip planner internal algorthim
What are the architectural differences between Erlang/OTP and OpenResty?
GTFS fares allocation depending on time
Using GTFS real time feed
How to run Elixir Supervisor in escript
Online Users Storing Elixir
How to create a distributed application on elixir?
What version of .NET does OTP.NET use? [closed]

Categories

HOME
sonarqube
eclipse
phpword
deep-learning
time-complexity
can
vxml
regular-language
python-multithreading
webix
fosrestbundle
ibeacon-android
watson-conversation
mod-security
windows-ce
shared-libraries
http-post
wolframalpha
jersey-1.0
springfox
exit
zxing
short-url
interpreter
gpio
jackson-modules
winexe
datastax-enterprise-graph
gp
twiml
zend-debugger
jbutton
openwhisk
fossil
sql-delete
navigator
raft
sim-toolkit
lint
opensmpp
stringtokenizer
datacachefactory
contract
rfc3339
snapchat
flask-restful
automapper-5
gitlist
dtsearch
trojan
wpas
android-testing
git-flow
seq
google-photos
filenet
sandcastle
juice-ui
windows-azure-queues
rspec2
hadoop-partitioning
invoke-sqlcmd
design-principles
django-urls
extjs-grid
rpart
typemock
baasbox
qtwebengine
adehabitathr
nodelist
administration
psr-4
paw
ray
memset
renderer
koala
conditionaltagsupport
accesscontrolexception
spring-3
font-awesome-4.0.0
paginator
infobright
screen-size
lr
svk
drupal-fivestar
interprocess
django-piston
wcf-instancing
fault-tolerance
entitykey
dsoframer
contentpresenter
online-storage
leader

Resources

Encrypt Message