Meteor subscribing based on specific template - coffeescript

I am working on meteor page where I am subscribed to two different sets up published data. I am subscribing to these in my router.
** Router **
#route 'chat',
path: '/chat/:room_name'
notFoundTemplate: 'home'
loadingTemplate: 'Loading'
waitOn: ->
Meteor.subscribe('rooms')
Meteor.subscribe('messages', #params.room_name)
data: ->
if Meteor.user()
Rooms.findOne({room_name: #params.room_name})
action: ->
if #ready()
#render()
Meteor is acting exactly as it should. By this I mean, whenever updates to rooms or messages are made, both of the templates on my page are updating their data. What I need is to separate this.
I.e. When an update to rooms is made, only my rooms template updates, and when an update to messages is made only my messages template updates.
I am assuming that I need to place my subscribes in specific areas to get this to happen, but I am not sure where to put them.

Related

Is there a smart possibility to get API results without sending requests every second? [VueJS | Vuetify]

So I made a website to show which services on my server are running and which are offline.
The site is an Vuetify App running in a docker container. My services are monitored via UptimeRobot.
Currently I use:
created: function () {
this.interval = setInterval(() => this.getStatuses(), 1000);
},
To trigger my API request function every second to update the status of my services.
But is there some smarter possibility to only update on change and not request every second to see if something happened?
Like I send one request to get the status and then receive a message when something changed? I hope you can understand, whats my problem. It's hard to decribe.
Yes you can by firing an event. for example:
in your app.js
window.Fire = new Vue();
For example here you create a user then you want to update table after creating a new user, Follow these steps:
createUser(){
// FireUpdate is your fire name, you can give it any name you want!
// Call this after you post something to specific route.
Fire.$emit('FireUpadte');
}
Then you will load new users using this approach:
created(){
// Load new Users after created.
Fire.$on('FireUpadte', () => { this.createUser(); });
}
For more information check this video: https://www.youtube.com/watch?v=DHuTkJzH2jI&list=PLB4AdipoHpxaHDLIaMdtro1eXnQtl_UvE&index=20
What you're looking for are websockets. You establish a websocket connection and it stays open, allowing the server to notify the web app when something changes.
You can run your own socket.io server on a Node.js backend or use a service like Pusher.com (very cheap, free tier is pretty big).
I highly recommend going the Pusher.com route, they also have great tutorials ; )
https://pusher.com

Structuring Firebase database like Snapchat

I've been working on a snapchat clone as a way to learn how to use firebase.
I am currently stuck on how to best structure my data so that I could mimic a simple version of Snapchat.
What I'd like to do in my simplified version:
Send a picture message to multiple users
Post the picture message to a "story" feed where all my friends can see it.
I don't need anything more really. I'm not trying to implement snapchats current feature of being able to send text messages or anything like that. I just want to send pictures to friends and also post them to a public feed.
I've structured my data like this:
And Breaking it down:
Users:
Friendships between users structured like this:
Individual messages structured like this:
An index for conversations like this:
Now I've seen plenty of posts on stack and online for structuring messages in chat applications. What I'm stuck on is how to structure my DB so that a message can be sent to a user so that only that users receiving users see it.
I've been reading the firebase docs and I know I should be denormalizing data so that I can read data more efficiently, but I can't really wrap my head around the best way to do so with Firebase.
In my current implementation of user-messages, I would have to implement a check in my code to see if the message was sent by my user, and then prevent the user from seeing it. Ideally only the person who the message was sent to should see the image. (just like snapchat)
Any suggestions on how to do so?
Do I need to have some reference to the chat/message in the user tree?
The question appears to be
how to structure my DB so that a message can be sent to a user so that
only that users receiving users see it
There are many 'directions' but here are two.
1) Manually 'tell' each user about a pic
users
uid_0
pics_for_me
pic_0
url: "http://...."
pic_1
url: "http://...."
my_users
uid_2: true
uid_1
pics_for_me
my_users
uid_0: true
uid_2: true
With this structure, uid_1 has two users they want to share pics with (uid_0 and uid_2) so when it comes time to share the pic, read the my_users node, iterate over it, and store a link to the pic in that users respective pics_for_me node. Obviously you could store other data such as who it's from etc. When uid_0 logs in, read the pic_for_me node and view the pics, perhaps deleting it when done.
2) Observe a node for pics for a user
users
uid_0
name: "Henry"
uid_1
name: "Joe"
all_pics
pic_0
for_user: "uid_0"
url: "http://..."
in this scenario, uid_0 logs on and adds a query to all_pics for any for_user that contains their user id. This would work for a 1-1 situation where the pic is meant for one user. If you want multiple users to get a pic then...
all_pics
pic_0
uid_0: true
uid_1: true
and when a user logs in add a query to all_pics where that users uid is true, and then any time a picture is added that has a child of their uid, they will receive an event.

How does feathers sequelize work

I'm using angular2 on frontend, node/feathers on backend over Postgres (sequelize) db.
On backend I have events service on ".../events" route, and I'm trying to reach data from db, it works fine when I want to retrieve all events so I just make request on this route.. or when I want to retrieve just one event with certain id (make request on "../events/>>id<<). But how to get data by other attributes, for example I have user and its in association with event one to many, so how would I retrieve all events for one user (search by user id, not by events id). Or any other attribute, for example by name, return all events with certain name. How would that work, any code example?
In events model I have relation with user as one to many (event has creator) and I made that connection as events.belongsTo(user). How can I add that this same event has also many to many relation with users cause it has also attribute "participants" as users that are also included into this event, and every user can participate or own many events.
Found it myself, just add ?attribute=value... so if i want all events owned by user with id 1... it would be ".../events?userId=1 ... and this would return me all the events with userId=1... same goes with any other attribute –

Ember CLI - Custom routing for very basic dropdown

New to ember and ember cli, and not having any JS based framework experience apart from jQuery (which is not a framework)
I find my self stuck at the very beginning compared to work done in Angular,
I have a static list of groups which are on REST api `http://localhost:8000/api/groups' and it is only 8 items there, I needed to show them as dropdown for a search criteria. Nothing fancy
I started with creating a route and model with the name of groups but app stopped working and I had to create a model for group which is identical to groups model, only having 2 items for dropdown
Now i have a url in my ember app http://localhost:4200/groups which I dont need and i dont want it to be there,
But I ignored it and had to create a dropdown of the cities, api is
http://localhost:8000/api/cities <-- All cities list, needed for admin
http://localhost:8000/api/cities/active <-- For clients to show active cities so they can add their records
http://localhost:8000/api/cities/filled <-- For users, to show them where clients are available
So in ember, I created a route and model for cities, and same model is copied as city just to show the list of cities in dropdown, I needed to show cities which are filled, I had created ember g route city/filled and it created folders, but its model is also same like other two models.
Ajax call is not being sent to city/filled url and now I ended up having
http://localhost:4200/cities // useless for me
http://localhost:4200/cities/filled //useless for me
and in filled i see that ajax call is made but to the http://localhost:8000/api/cities two times, loading same data. I tried adding a dropdown on my application and opened http://localhost:4200/cities/filled in browswer, and woosh, it send ajax call to the same url 3 times, one for application.hbs and one for cities.hbs and one for city/filled. Why load the same data 3 times if it is already fetched from same url within single request?
in angular I just call a custom url and I can get the data, but for ember its really hard to get grip on these small things and there is no help around
active and filled are filters for your cities resource and these are not standalone resources, so you should try to use them as query parameters. like
http://localhost:8000/api/cities?type=filled
http://localhost:8000/api/cities?type=active
Facebook uses this style for query params. You can also use query params for getting paginated data of a resource, like for 1st page and 25 records per page, the request will look like:
http://localhost:8000/api/cities?page=1&records=25

Where does related logic go on model creation when working with REST api with Django, Backbone, and Tastypie?

We are trying to move some of our app to use backbone and tastypie. I have the REST api set up and it is working on some basic examples. However, there are a few issues where currently we post an ajax request to a custom url, and in that view do a few things like
make related objects
call a few related functions
However, now that I've switched some of this functionality to using backbone and the REST api, I'm not sure where all of that should go!
For example, I had a view to make a Message, and when I made a Message, I also made a Notification and called a function to add some points to the user. Something like
def ajax_send_message(request):
## ... set up some variables ...
## Make the new message
message = Message(user=user, content=message)
message.save()
## Make the notification
notification = Notification(message=message)
notification.save()
## Give the user points
user.add_points_for_message();
return json_response({"status": "ok"})
Now--am I just supposed to do this all in JavaScript? I have a Message Backbone model as well.
// Create message backbone object
var msg = new Message({content:content, user: user});
// Post to server
msg.save();
// Add to backbone collection
messages.add(msg);
I've looked at different parts of tastypie, and it seems you can create custom URL endpoints, and also do validation, but that doesnt seem like the right spot to call related methods. It seems that calling related methods goes against the REST part of it---but then where are they supposed to go?
If I want to add some logic to backbone only when an object is created, where does that go?
The first thing I would suggest is to switch your mindset to an event-based model, where your code reacts to events. In your example above, you save the model to the server then immediately dd it to the collection. How do you even know that the model was saved correctly? That procedural style of programming works better in a synchronous, server-side style of programming.
In the asynchronous world of client-side programming, you make a request and then set up callbacks which determine what will happen next, depending on the events your are listening for. In your case, you want to react a certain way when the message is saved successfully, correct? You can define a success callback for your save operation like so:
msg.save({
success: function(model, response, options) {
messages.add(model);
// code to add notification
// code to add points
}
});
Basically, you are saying "I would like to save this model, and then listen for a success event. When the event comes in, execute the following code." Notice also that I am adding the model returned from the API to the collection, since this is the exact object that was persisted to the server so it's more appropriate to add than the model you created.