{"id":3848,"date":"2012-12-20T09:00:51","date_gmt":"2012-12-20T17:00:51","guid":{"rendered":"http:\/\/localhost\/exoblog\/?p=3848"},"modified":"2023-07-27T16:46:14","modified_gmt":"2023-07-27T14:46:14","slug":"chat-in-the-cloud-with-mongohq","status":"publish","type":"post","link":"https:\/\/www.exoplatform.com\/blog\/chat-in-the-cloud-with-mongohq\/","title":{"rendered":"Chat in the Cloud with MongoHQ"},"content":{"rendered":"<p>Going back in time, I published a blog post about a new eXo Add-On: eXo Community Chat.<br \/>\nYou can read more about it here: eXo Platform Add-ons: Chat in Space<\/p>\n<p>As I promised some technical posts about the technology I used, I wrote a first post about MongoDB: Chat Application: Why MongoDB?<\/p>\n<p>Now, it&#8217;s time to see all the coolness of MongoDB inside an eXo app. We will see in this post how easy it is to switch to a Cloud storage using MongoHQ service and what are the impacts for your app.<\/p>\n<p><!--more--><\/p>\n<h2>Documents Storage in the Cloud<\/h2>\n<p>We all know &#8211; developers &#8211; that running a same web app locally and on a production server is not the same in terms of response time. Locally, you have everything on the same machine. When you&#8217;re in Production, you have different setups, network lags, etc.<\/p>\n<p>But more than that, we&#8217;re now living in a <em>must-be-Cloud-ready<\/em> world. You develop in the Cloud &#8211; <a href=\"https:\/\/cloud-ide.com\/\" target=\"_blank\" rel=\"noopener\" class=\"broken_link\">Cloud IDE<\/a> &#8211; share your work in the Cloud &#8211; <a href=\"https:\/\/github.com\/benjp\" target=\"_blank\" rel=\"noopener\">GitHub<\/a> &#8211; deploy in the Cloud &#8211; <a href=\"https:\/\/www.cloudbees.com\/\" target=\"_blank\" rel=\"noopener\">CloudBees<\/a> and Co &#8211; run in the Cloud &#8211; Cloud Workspaces &#8211; and store every single piece of it In The Cloud &#8211; <a href=\"https:\/\/www.mongohq.com\/home\" target=\"_blank\" rel=\"noopener\" class=\"broken_link\">MongoHQ<\/a>. Isn&#8217;t it clear enough? \ud83d\ude42<\/p>\n<p>So, I decided to give MongoHQ a try and see what the <em>user experience<\/em> would be if development was done this way in production.<\/p>\n<h3>Lag: Manage it the right way<\/h3>\n<p>I don&#8217;t blame the internet for being slow or MongoHQ for giving me a shared\/slow machine for my free subscription. When I choose to take a free service, I choose to pay the price for this&#8230; and it means: <em>no <a href=\"https:\/\/www.redswitches.com\/dedicated-server-hosting\/\" target=\"_blank\" rel=\"noopener\">dedicated server<\/a>!<\/em><br \/>\nI think it even has a great side: you must think about your app differently as it will lag from time to time. So, you must respect that so the end-user experience doesn&#8217;t suffer.<\/p>\n<p>MongoHQ comes with a great user interface (another one using responsive design &#8211; I love that).<\/p>\n<p><a href=\"https:\/\/www.exoplatform.com\/blog\/\/wp-content\/uploads\/2012\/12\/mongo-12.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-3849\" title=\"mongo-12\" src=\"https:\/\/www.exoplatform.com\/blog\/\/wp-content\/uploads\/2012\/12\/mongo-12.png\" alt=\"\" width=\"650\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12.png 861w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-300x162.png 300w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-768x415.png 768w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-720x389.png 720w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-500x270.png 500w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-360x194.png 360w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-200x108.png 200w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-100x54.png 100w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-12-56x30.png 56w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/a><\/p>\n<p>If you work with MongoDB, you certainly know about MongoHQ but if you don&#8217;t, give it a try, it&#8217;s damn easy to set up. So, not that much to do on my side. I wrote a PropertyManager so I can get properties stored in the tomcat\/conf. So, I just switched from:<\/p>\n<pre class=\"lang:apache decode:true \">dbServerHost=localhost\r\ndbServerPort=27017\r\ndbName=chat\r\ndbAuthentication=false\r\n<\/pre>\n<p>To this:<\/p>\n<pre class=\"lang:apache decode:true \">dbServerHost=alex.mongohq.com\r\ndbServerPort=10005\r\ndbName=chat\r\ndbAuthentication=true\r\ndbUser=admin\r\ndbPassword=dontimagineiwillgiveyoumypassword<\/pre>\n<p>It&#8217;s all mentioned on MongoHQ when you create your database.<br \/>\n<a href=\"https:\/\/www.exoplatform.com\/blog\/\/wp-content\/uploads\/2012\/12\/mongo-13.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-3850\" title=\"mongo-13\" src=\"https:\/\/www.exoplatform.com\/blog\/\/wp-content\/uploads\/2012\/12\/mongo-13.png\" alt=\"\" width=\"650\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13.png 692w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13-300x152.png 300w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13-500x253.png 500w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13-360x182.png 360w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13-200x101.png 200w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13-100x51.png 100w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-13-59x30.png 59w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/><\/a><\/p>\n<p>Once it&#8217;s done, you&#8217;re ready to go, just reload the server and you&#8217;re now connected to a database in the Cloud. It&#8217;s that easy!<br \/>\nElegant, isn&#8217;t it?<\/p>\n<p>On the Java side, it looks like this:<\/p>\n<pre class=\"lang:java decode:true \">String host = PropertyManager.getProperty(PropertyManager.PROPERTY_SERVER_HOST);\r\nint port = Integer.parseInt(PropertyManager.getProperty(PropertyManager.PROPERTY_SERVER_PORT));\r\nm = new Mongo(new ServerAddress(host, port), options);\r\nm.setWriteConcern(WriteConcern.SAFE);\r\n...\r\nboolean authenticate = \"true\".equals(PropertyManager.getProperty(PropertyManager.PROPERTY_DB_AUTHENTICATION));\r\nif (authenticate)\r\n{\r\n\tdb.authenticate(PropertyManager.getProperty(PropertyManager.PROPERTY_DB_USER), PropertyManager.getProperty(PropertyManager.PROPERTY_DB_PASSWORD).toCharArray());\r\n}<\/pre>\n<h2>JavaScript is great too<\/h2>\n<p>On the client side, I also ask myself a few questions. The easy answer for a Java developer is often when he designs an app is to use one way to develop and stick with it. I&#8217;ve seen that a lot.<\/p>\n<p>So, you decide to write a JSF\/Hibernate application. Each time you have a human interaction you use JSF Controllers.<\/p>\n<p>With libraries &#8211; frameworks &#8211; like JQuery, you can actually do a lot on the client side. The bonus is, everything done on the browser doesn&#8217;t need to be on the server.<\/p>\n<h2>A server call is not needed when it&#8217;s not needed<\/h2>\n<p>I will give two examples: I get the room data as a simple JSon feed with a jQuery Ajax call:<\/p>\n<pre class=\"lang:java decode:true \">{\"room\": \"50bf230ac51064038e31cedf\",\"timestamp\": \"1354711914497\",\"messages\": [{\"user\": \"john\",\"fullname\": \"John Smith\",\"date\": \"05\/12\/2012 11:44 AM\",\"message\": \"alors\r\n\"},{\"user\": \"john\",\"fullname\": \"John Smith\",\"date\": \"05\/12\/2012 11:44 AM\",\"message\": \"How are you?\r\n\"},{\"user\": \"mary\",\"fullname\": \"Mary Williams\",\"date\": \"05\/12\/2012 11:45 AM\",\"message\": \"I'm great\r\n\"},{\"user\": \"john\",\"fullname\": \"John Smith\",\"date\": \"05\/12\/2012 11:57 AM\",\"message\": \"cool\r\n\"},{\"user\": \"james\",\"fullname\": \"James Davis\",\"date\": \"05\/12\/2012 01:51 PM\",\"message\": \"Hi guys\r\n\"},{\"user\": \"john\",\"fullname\": \"John Smith\",\"date\": \"05\/12\/2012 01:51 PM\",\"message\": \"Hi James\r\n\"},{\"user\": \"mary\",\"fullname\": \"Mary Williams\",\"date\": \"05\/12\/2012 01:51 PM\",\"message\": \"Hi James\r\n\"},{\"user\": \"mary\",\"fullname\": \"Mary Williams\",\"date\": \"05\/12\/2012 01:51 PM\",\"message\": \"How are you ?\r\n\"},{\"user\": \"james\",\"fullname\": \"James Davis\",\"date\": \"05\/12\/2012 01:51 PM\",\"message\": \"I'm fine\r\n\"},{\"user\": \"james\",\"fullname\": \"James Davis\",\"date\": \"05\/12\/2012 01:51 PM\",\"message\": \"thanks\r\n\"}]}\r\n<\/pre>\n<p>In terms of server calls, it&#8217;s the most efficient way of doing it I think.<br \/>\nThen, all the formatting, decoration, beautifiers is done with JS code, to get something like this:<\/p>\n<p><a href=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3851\" title=\"mongo-08\" src=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08.png\" alt=\"\" width=\"340\" height=\"226\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08.png 340w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08-300x199.png 300w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08-197x131.png 197w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08-100x66.png 100w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-08-45x30.png 45w\" sizes=\"(max-width: 340px) 100vw, 340px\" \/><\/a><\/p>\n<p>Another example of the search. For now, I will stick with a local JS search.<br \/>\nAs JSon messages are stored locally, the search uses them and is then decorated and redrawn using jQuery.<\/p>\n<p><a href=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3852\" title=\"mongo-09\" src=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09.png\" alt=\"\" width=\"347\" height=\"403\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09.png 347w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09-258x300.png 258w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09-282x328.png 282w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09-203x236.png 203w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09-113x131.png 113w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09-77x90.png 77w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-09-26x30.png 26w\" sizes=\"(max-width: 347px) 100vw, 347px\" \/><\/a><\/p>\n<p>One question we always have to ask ourselves now is: Do I really need a server call to do this?<br \/>\nIn this search scenario, I originally did a server-side search. But, it wasn&#8217;t as fast as I wanted it to be. So, the downside for now is I don&#8217;t search on all rooms. In the future, if I want to do cross room searches, I will either update and store locally using HTML5 storage (my 2 cents on this) or I will do a server call. This still has to be tested to choose the best solution.<br \/>\nIn the end, if you take a look at my code, you will see a lot of jQuery in it (but not that much).<\/p>\n<h2>Asynchronous is the key<\/h2>\n<p>On the client side, as MongoHQ is not as fast as a local server, I asked myself also how I could send feedback without being affected by possible lag.<\/p>\n<p>Thus, when you log in, you have a Waiting Connection panel until everything is displayed (like on Skype by the way).<br \/>\n<a href=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3853\" title=\"mongo-11\" src=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11.png\" alt=\"\" width=\"160\" height=\"161\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11.png 160w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11-150x150.png 150w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11-50x50.png 50w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11-35x35.png 35w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11-130x131.png 130w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11-89x90.png 89w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-11-30x30.png 30w\" sizes=\"(max-width: 160px) 100vw, 160px\" \/><\/a><\/p>\n<p>For a regular web page, it wouldn&#8217;t be that important but we&#8217;re talking about a near-to-real-time application here, thus, we must give some feedback to create a better user experience.<\/p>\n<p>Another example is when I post a message. I first add it to the current JSon tree and display it. Then, I use an async Ajax call to send the data and get it back to update the chat window with all updates.<\/p>\n<p><a href=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10.png\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-3854\" title=\"mongo-10\" src=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10.png\" alt=\"\" width=\"650\" height=\"130\" srcset=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10.png 650w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10-300x60.png 300w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10-500x100.png 500w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10-360x72.png 360w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10-200x40.png 200w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10-100x20.png 100w, https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2012\/12\/mongo-10-70x14.png 70w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Next year :), I will write a final post on the Chat application about Juzu and how you can easily write both a Java Portlet and a standalone application with this great web framework.<\/p>\n<p>If you read these posts to the end, I first give you a big thank for your reading time.<br \/>\nYou want to know more or help me to build an even better Chat application? Contact me or simply fork the Chat GitHub project, I&#8217;ll be more than happy to integrate new cool ideas in the Chat (or bug fixes).<\/p>\n<p>Thus, if you want to take a deeper look at the code, it&#8217;s Open Source and available on eXo Addons Repositories: <a href=\"https:\/\/github.com\/exo-addons\/chat-application\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/exo-addons\/chat-application<\/a><\/p>\n<p>\u00ab Again, thanks for reading and stay tuned \u2013 more things are coming, Benjamin. \u00bb<\/p>\n","protected":false},"excerpt":{"rendered":"Going back in time, I published a blog post about a new eXo Add-On: eXo Community Chat. You can read more about it here: eXo Platform Add-ons: Chat in Space As I promised some technical posts about the technology I used, I wrote a first post about MongoDB: Chat Application: Why MongoDB? Now, it&#8217;s time [&hellip;]","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[699],"tags":[],"lang":"en","translations":{"en":3848},"pll_sync_post":[],"_links":{"self":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts\/3848"}],"collection":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/comments?post=3848"}],"version-history":[{"count":0,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts\/3848\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/media?parent=3848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/categories?post=3848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/tags?post=3848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}