How would you advise managing likes/unlikes on an activity object? - javascript

For example, if I were to build an application similar to Instagram where each post displays the number of likes for each activity.
What streams would I need to create?
What would the network calls look like for liking and unliking an activity?
Where would I get the number of likes associated with each post?
Can I do this all on stream, or would I need to rely on another backend?

There are two approaches you can take. One updates the original activity with a counter, another uses our analytics.
When you add an activity, if you send us a foreign id and timestamp, you can use that foreign id later to update the activity. Then, in the metadata of the activity you can have a field to count the likes and each like/unlike would update the activity. If you look at the Instragram-clone project I built in late 2016 you'll see an example of this in the Go backend code. The pros/cons of this approach: easy to manage yourself, but you need to send the entire activity as it was (same timestamp, and all other fields as you originally sent it) so you'll need to keep the data on your side as well. Also, if a user follows the feed where that activity is stored, they may not see the updated content as a new activity in the feed.
The feeds you'll need: https://getstream.io/blog/best-practices-for-instagram-style-feeds/
Backend code: https://getstream.io/blog/example-go-service-for-photo-sharing-app/
The second approach uses our analytics properties that can track this kind of metadata field in a different way, but the down side is that it's only available on our enterprise plans. You can look at our documentation page for information on how to use this.
Introduction: https://getstream.io/analytics/
Documentation: https://getstream.io/docs_analytics/#introduction

Related

Tracking purchase of particular product on different sites

I have a website on which other websites owner can list their products. For listing the product they need to manually create the product by providing title, description, image and link of products.
When any user will visit my website he will be able to view these products and on click of any product he will be redirected to the owner's website and purchase will be done on his website.
Now I need to build a functionality by which I can track the complete transaction of the sale of that particular product, that particular product has been sold or not.
Whenever any site owner is coming on my site to list his product he needs to first register on my site.
After registration, I can provide him a chunk of a script that he needs to put on his site header.
Apart from this, I cannot modify his site. And I just need to track the particular product's transaction.
I have searched and found that Trivago and Skyscanner are using something like this.
I have tried to create some scripts in JS but couldn't track the desired things, as sometime user does not purchase my item and I did not know about this. In some sites, thank you page does not have enough information about the sale to capture.
If that can be possible just by adding few more things on Marchent's website please let me know.
To make sure that your Postback works on all platforms and providers, you must provide more than one way to your merchants to implement on their websites.
JS script ( you already done that )
Server to server implementation (S2S callback) - where you send the order id in the headers or get parameters, and the merchant must provide it back.
Example: you send your traffic as below format:
http://merchant_url.com/?tracking_id = 123123123
The merchant returns back when a purchase is made to your tracking url:
http://your_tracking_url/?merchant_id=1&tracking_id=123123123
This way you can identify your traffic
1px iframes, that load on their thank you page and they pass the click order_id parameters
Example: your merchant should place something like the below on their thank you page:
<iframe src="http://your_tracking_url_iframe/tracking_id=123123123"
style="height:1px;width:1px"/>
lastly, even 1px image elements are usually also used in such cases.
Example:
<img src="http://your_tracking_url_img/?tracking_id=123123123" style="height:1px;width:1px"/>
This way, even if merchant is using simple html/js on their thank you page they can always load your iframe with the specified parameters which will help you track a sale.
Hope this helped.
You can use cookies for an easy implementation.
Because the end client need to come from your website he should have your cookie with a userId and a productId before he goes to an other website.
On the thank you page of the other website there should be a call to your server (usually a 1px image). Server side you will have the same cookie and the website as referer.
Then you can say to the website how many clients bough after clicking on a product on your service. (Be sure to count sells only once per user!)
If the website want a cross validation they can provide you with the IDs of products bough when they call so you count only when the IDs match.
This is complicated, not because of the technology involved, but because of the variety of commerce solutions out there and the open-ended nature of human choice involved.
It sounds like you have secured two vital components to make this work: the ability to identify registered merchants and the ability to place a script on their webpage.
There is a third component you need, I think; either an agreed upon interface for that script (once a commercial transaction is completed or failed, hand the object with statuses back to your script through a specific triggered event) or full knowledge of the events for the merchant's website that you can code to.
Coding for the unknown will require a lot of time and effort as you would need to learn each merchant transaction solution and how to capture the transaction data. This will be... a long haul and I don't think it would be very successful.
If the merchant site is willing, they can trigger an event that your script will be listening for and pass along the transaction data to it, which would allow your script to pass along via AJAX to a waiting tracking page to record the results. This is simplest in terms of reaching an agreement and for the work involved, from your indicated starting point. jQuery is an excellent library for wiring this all up and there are other options.
Part of tracking would be passing along a token that ought to be carried through the transaction and passed back, generated by your site on the click to said merchant's website and passed on from there. Once you get your token back, you can compare it to a database of transaction tokens to find out which event had what result and fill in the appropriate fields from the resulting data.

Getting live notifications when users register

I'm looking to create a notification system for when someone registers to my site. The type of notification system I am wanting is something like stackoverflow has or Facebook. I want a number type notification to appear of how many new registrations I have, but once I click to view the notifications (new registrations) I want the notification alert (like the red square stackoverflow has) to go away.
I am writing this with PHP and have a db table called users that I can pull the users from. I'm just not sure how I can have a notification like this without selecting all of the users to give me a quantity?
So say I have 20 users and 2 new people just registered. I want a notification to display with the number 2. Then when I click on it, for that notification to go away.
How could I go about doing this?
I am designing systems like this.
The way I do it is to have a structured log of events written by the application into a messaging queue/message broker.
Then I have a message consumer that reads the events and updates any metrics I want out of the system.
In this case, you would need a message consumer that decides on what users should receive what messages (some messages could go to all, some only to specific people).
Each user would have to either poll an API that serves these messages, or have a websocket open to receive the data as a stream (this is faster).
I would place an intermediate storage between the message consumer and the API. I would recommend something light and fast, like Redis, for this.
This is a lot of infrastructure to set up, there are other quick and dirty solutions, but this is quite minimal in my opinion.
Assuming each users have an unique ID, you can achieve it very simply by yourself adding very few PHP code.
Answer will definitly depend on your own needs and complexity degree you want to achieve, #firelynx answer can be really great if you want to achieve something big and evolvable (you can definitly have some great fun and learn a lot).
However if you only want a very basic implementation I suggest you to create a "notifications" table in your database, you can use (as a minimum) columns [id, user_id, message, new] where new is a tinyInt or any equivalent.
Then add some notifications to the user(s) of your choice, new should be 1 by default. When the user see the notification, just set new to 0 for related notification(s).
Selecting users that should receive those notifications can then be done depending on user group or some conditions.
If you wanted something simple and not so shinny to begin with, that should do the job well, you can also add a "icon" column to add some fun.

How to handle this typical case of WebSocket usage?

I wrote a web page where there is a zone for user's comments.
Any authenticated users could post a comment.
As many users could post comments almost simultaneously, I want the comments list to be auto-refreshed.
Thus, I think about using WebSockets.
My thought are about a good/best practice for this use case:
Once a comment is posted, should WebSockets process read the current comments list on database and send a Json response containing all the new comments? This would allow the client to directly append the new comments on the DOM (JS).
Or should WebSocket just check the database (or queue if using a message queue (Redis, RabbitMQ etc..) for instance) and act just like: "Hey, I have new comments, click here if you want to see them !". This solution would only signal the presence of new comments, without bringing all those comments to the client. The workflow of retrieving the events would then involve by the client (by clicking on this sentence for instance) e.g using the traditional Ajax direction: client => server.
It is highly possible that a user posts a comment, then navigates to another page of the website. Therefore, a websocket response containing the whole new comments would be useless. A simple notification would then be possible, as most of known websites do for instance with the "+1" counter or more relevant to the "comments" scenario: "1 new comment available".
Which way should I choose?
I think to decide which data to push is mostly a matter of UI usability / user experience, as opposed to which technology is being used to interact with the server. We should avoid changing the UI with server pushed data in a way that would surprise the user in a negative way, for example having the comment feed constantly growing without any intervention from him.
But in the case of a realtime chart, it's probably better to push the data directly into the chart, that would be what the user expects.
In the case of the comment feed the reason why most sites go with the 'click to load' approach is because of user experience, so I think that is probably the best approach.
I use a combination of both....
In some pages the websocket communication contains the actual data--sort of like a stock ticker update.
And in other cases, the websocket communication just says -- all users viewing xyz data--refresh it. And then the browsers performs an ajax to obtain the new data and the grid is smartly refreshed in such a way that only the changed cells are modified on screen using innerHTML and the new rows are added and deleted rows are removed.
In cases like stackoverflow, it makes sense to show a message, "Got new stuff to show--want to see it?"
When I establish the websocket in the browser, I pass a page Id in the url and the cookies are passed too. So websocket server knows--the user cookie and the page which is being viewed.
Then in the database (or middle tier logic) communicates to the websocket server with messages such as: This message is for users viewing 'xyz' page: smartly refresh grid 'abc'. And the websocket server broadcasts the message.
Because the protocol allows you to pass anything you like, you have the ability to make it anyway you like.
My advise it to do what's best in each particular situation.

Tracking online status?

I am quite new to web development and am working on this social networking site.
Now I want to add functionality to show if a person is online.
Now one of the ways I figure out doing this is by keeping online status bit in the database.
My question is how to do it dynamically. Say the page is loaded and a user (say connection) comes online. How do I dynamically change status of that connection on that page.
I wanted to know if there are any tools(libraries) available for this type of tracking. My site is in python using django framework. I think something can be done using javascript/ jquery . I want to know if I am going in the right direction or is there anything else I should look into?
Create a new model with a last_activity DateTimeField and a OneToOneField to User. Alternatively, if you are subclassing User, using a custom User in django 1.5, or using a user profile, just add the field to that model.
Write a custom middleware that automatically updates the last_activity field for each user on every request.
Write an is_online method in one of your models that uses a timedelta to determine a user's inactivity period to return a boolean for whether they are online. For example, if their last_activity was more than 15 minutes ago, return False.
Write a view that is polled through jQuery ajax to return a particular user's online status.
As Sanjay says, prefer using memory solutions (online statuses have a quite brief use) like the Django cache (Redis or Memcache).
If you want a simple way of updating the online status of an user on an already loaded web page, use any lib like jQuery, AJAX-poll an URL giving the status of an user, and then update the tiny bit of your page showing your wanted status.
Don't poll this page too often, once every 15 seconds seems reasonable.

Automatic userlist update

I have a online user list which is populated by a SQL query to a database table.
When a new user comes online, how can i make the webpage automatically update?
What code do I need to provide?
Thanks
There are a few ways you could do this, but I'll avoid the most tempting WebSockets HTML5 new-ness and suggest the following:
When a user logs on, record the fact that they are logged on to the database.
From your page, poll a service or web page method that lists online users.
If the list changes, update the part of the page that shows the users.
That's the rough outline of what to do. If you need specifics please say what server-side host and programming language you are using. From the client side, please also mention what JavaScript framework you'd prefer or are open to.

Categories

Resources