Live Page View Counter in Rails - javascript

I'm currently looking for a way in Rails/Javascript to show a live count of all users visiting a specific controller action. Think of it like a similar functionality to Twitch.tv channel viewers. I've seen some older instances of ways to accomplish this, but I'm new to rails and back end code in general, so I'm not exactly sure the best way to even think about this problem. Maybe there's a gem out there that would help me as well. If so, I'm definitely open to it.
Thanks in advance for taking the time to help me with this!

I strong suggest that you take a look at ActionCable feature. Its native, so you don`t need any extra gems and, with a little bit of code, you can get real time feautures up and running.
Docs
http://guides.rubyonrails.org/action_cable_overview.html

Related

Django schedule framework doesn't work like it's supposed to

I'm trying to get to work the schedule framework for django but it doesn't look right and it's functually broken as well, although only in the front-end. My guess would be that Javascript (jquery?) doesn't work correctly.
This is how it is supposed to look like
vs
this is how it actually looks like
Does anybody have an Idea what the Problem might be?
Any help would be much appreciated.
I did some digging in the framework's github issue page and apparently there are lots of people with the same issue. I have come to the conclusion that just looks like this out of the box and that I did nothing wrong (at least to cause this look). I will just try to make my own views and templates.

Cramming tons of HTML in to one page

I am helping to design a HTML client for a game that I am collaborating with others on.
This client is going to need to have multiple scenes/pages like the login, the lobby, the actual game page, etc.
Normally, I would be just fine with navigating in and out of pages. However, the client needs to have a constant connection with the server via a Websocket, so therefore it cannot navigate away from the page.
Option 1: Put everything in one file
Rather than having each scene in its own separate page, just cram all the HTML together.
Then, when one scene is needed, simply hide all the other scenes.
I do not think that this way is the way to go: from what I know about HTML and web practices, this is not a very smart practice.
Option 2: jQuery's .load()
Using jQuery's handy-dandy .load method, an external HTML file can be easily loaded into the current HTML file.
To me, this seems that this might be the best way. However, I am not very familiar with how this method acts, so I do not know if this will cause bumps in the road ahead.
Option 3: ???
This is where I need help. Unless one of the two above options is the best way, what is the best option for my situation?
Notes
I am not looking for speed here; it's okay if there is some delay between loads.
Not sure about your backend but it seems like you are looking for a single page app solution.
I recommend AngularJS, currently maintained by Google.
Others have mentioned Angular, but I'll just throw out the recommendation of KnockoutJS as well. Knockout has a less steep learning curve than AngularJS, and though it doesn't have as many features as Angular does, the latest version does have better browser support.
Both Knockout and Angular have excellent documentation and tutorials available via their websites.
I would recommend Angular 2. Since you seem like a beginner in front-end (correct me if I am wrong), starting with Angular 2 would be great over 1 because then you can learn this really good JS framework only once. It will help you keep everything organized and will prevent you from making your markup messy.

How do I create a very simple webhook?

First off, go easy on me. While I have been doing extremely basic coding since I was a child, it's only been over the last few months that I started getting into the real stuff like Java, Javascript, PHP/MySQL, etc. So please bear with me if I use wrong terminology or write something that sounds completely wrong. I'd love to get some insight from all you tireless helpers here.
Here is what I need:
I have a website at myserver.com.
I want to use the API at randomsite.com to automate a process.
If I manually go to the URL randomsite.com?do=this, my process will be triggered.
What I want to be able to do is write an app on myserver.com that will trigger randomsite.com?do=this. Obviously I can have a link on my page, but the user will see that they have been directed to randomsite.com.
I do not need to capture anything about the randomsite.com link having been triggered. I don't care who clicked it, etc. All I want is to simulate the effects of going to randomsite.com?do=this but without actually opening that site on the front end.
Thank you so much for your help!
Consider using AJAX for doing stuff behind the scenes...
https://api.jquery.com/jQuery.ajax/
EDIT: You would need to use JSONP for cross domain.

Javascript best practices with web sockets

I am going to make my website more interactive with websockets to update content without having to refresh. On server side, my Play! application is well structured, I have a controller per page. But in javascript, would you write a file for each page like
websocket_page1.js ...
websocket_page2.js
...
Or something else ?
I will have stuff like a header with facebook like notification icons, some feeds with infinite scrolling, some action buttons.
More generally, is there any JS code writing convention like there is a structure for the Play! project ?
thanks,
Julien
There is no preferred way to structure your Javascript in Play. This imposes some more work upon you, but also leaves you more flexible. If you need an opinionated approach to Javascript, there's a lot for you out there: from backbone.js to AngularJS. Some are easier to use, some are smaller, some provide a stronger structure. Just have a look through them.
You should also at least have a look at Ractive.js. Haven't used it so far, but it sounds awesome.
But, well, to come back to your question: you don't need some JS framework. Just do what you think fits your project. These frameworks might help you, but at the end of the day, it's up to you.

Creating a Notification Icon with Dojo 1.9

I want to create a notification icon with Dojo, but I'm not sure where to start.
The icon has to be similar to those found on Facebook and Whatsapp, showing how many new messages/notifications you have.
I've been looking in the Dojo reference, but couldn't find a good widget to use. Maybe I'm missing something obvious, but I might as well ask it here.
Googling hasn't been of much use unfortunately.
As far as I know there's no widget that automatically does the stuff for you. This problem is far too specific to provide a general widget for it. You also have to think about several things before you can start.
Do you want to use WebSockets? The web is generally made for pulling content, if you want to push your notifications from your server to your client, then you will need to look at WebSockets. A good tutorial to use them can be found on Sitepen.
If you don't want to use websockets (you're not able to create a websocket server or you have some other reasons) then you will probably have to use a simple "REST service" and obtain the amount of notifications from it. This means you're script won't be "live", so you will need to pull the content from the webservice every X seconds/minutes. This means you need a normal AJAX request which can be done with the dojo/request/script module.
If you need to time this AJAX request, you can use the dojox/timing module.
This is only the "logic" part, the user interface (icon + # notifications) can be made by yourself with plain HTML and CSS.
What about dojox/mobile/Badge:
http://livedocs.dojotoolkit.org/dojox/mobile/Badge
?
From comment:
"Maybe you can find something in the DojoX library" -
off course you will find much.
I would recommend
dojox/socket
see http://dojotoolkit.org/features/1.6/dojo-websocket

Categories