How to use redis for notifications in NodeJS/MongoDB? - node.js

I have running on NodeJs/MongoDB and Android/HTML client where people are able to follow posts, I store their ids in an array of followers in the post's document.
I want to implement a notification feature when a new comment comes I want to notify all the followers.
So, I have looked across the web but I couldn't get one tutorial where I understood exactly how things work.
I have seen many recommend the Redis Pub/Sub, but from what I have read Redis is a in-memory database, so I was thinking, I could create a channel in Redis with the post's Id, when the user follows that post I would subscribe him to the channel, then when a new comment comes i just publish to the channel. Is this how it works?? If so, then how about when the user goes off then comes back in later, And how about when there are many posts, won't Redis clear the channel then I wouldn't be able to send the notification to all the followers?
I think I'm understanding it all wrong,I'd appreciate your help. How exactly can I be able to send a notification to all followers?

Related

How to make twitter bot track tweets from our followers?

I was making a twitter bot with Twit, I found making tutorials using T.stream('user')
to get stream data about our account, now its deprecated. So in 2021, how can i achieve these?
requirements:
Know when someone follows my bot, and make a thanks post.
Know when someone in my followers list or following list makes a tweet.
Also I have a doubt, when we use stream, old data is also coming, so how exactly stream works? if i only want latest tweets, I have to sent get by setting interval?
For the new followers, you will need to build something using the Account Activity API, which uses webhooks to send you event messages. You will need to filter for the follow events.
For new Tweets from people you are following, you will need to poll the home timeline endpoint. There’s no realtime method for this.

Best way to implement Notification system using Nodejs and Postgresql Database

currently i'am developing a react app where user can post and comment and like a post.
so i did everything but there is one more thing that i want to do which is getting notification after user
Post or maybe comment and so on..
so let say i have a table called 'posts' in the database (PostgreSQL) and when user called "A" create post ,that post inserted into the database and then i want to receive a notification which Said that the user "A" created a post with the ID of that post and i think the main idea here is to watch the table of posts and whenever a new raw inserted a notification fire.
i searched many sources and find that i can use Pusher ,web-socket but to be honest i can't make a decision without figuring out the best way to achieve that.
So How i can get notified hence a new row inserted into the table "posts" ?
Thank you in advance.
Postgres has LISTEN/NOTIFY that you could use in conjunction with an on-insert trigger on the posts table.
Some process can then be listening for those notifications and figure out whom to send the notification to (via whichever transport you choose).
However, I think you might be better off adding a concrete table for notifications, so the user can see notifications that occurred when they weren't connected to the notification transport.

How to get real time feed to all the follower in node application

I am developing Social network type of application in node.js and MongoDB.
I want to add a functionality such as -
If user A has followed user B then when user B post something it should automatically come to user A on his wall!
Approach 1- This is what I have research till now
Create a channel of user B and all the other user subscribe to that channel and when user B post something then emit that with socket.io on all the subscribers.
so for this approach, we are thinking to create a channel when the new user signup every time.
But the problem is -
is this feasible approach as for how do I know which channel is which user or is there a way to store the channel in MongoDB.
if I create too many channels will that crash my server?
Approach 2 -
Another Approach that I have got is
Look up the followers of the source user in your database.
And emit feeds to all the followers using socket.io
How to scale this if I have got 100 followers of each user and when everytime user post, will searching in a Database creates an overhead.
Using Redis caching would be better to store followers?
And instead of checking the followers-
find that from Redis and emit the msg using socket.io
Approach 3 -
I have heard the name of Redis pub/sub but not able to find how it will work in my application.
Please suggest some best standards for creating something like this!
And if above approaches are fine can you pls suggest me the flow for that or how do I create a code for that!
I would be great if you provide me an example.
I think you can use Approach 2, but you must create a queue (redis queue or rabbitMQ) to do that.
I mean when you create a post, it must push to queue to push message to 100, 1000 follower in your DB.

Socket io rooms (MEAN stack)

I am building a buy and sell website that is built on MEAN stack. Im planning to use socket.io. So here's how my website will work
User will register and login
They can post an item either buy/sell
Other users can offer to the post (i'll use socket.io on this so that the user who posted the item will have a notification)
The poster will have an option to view the current offers, then they will choose who'll they accept.
Once they accept the offer, both the Poster and the User who offered will have a communication both of them (Chat room)
So when the poster accepted the offer of the customer, it will open a chat for them to communicate. So here's my question, i will use socket io rooms. Once the poster accepts the offer, both of them will join the room (so i'll generate a room for them and automatically join them) then this room will be saved to the MongoDB, then every message they'll send, it will be saved to the database. So that they can see the history of their message even though they logout.
Is this the best method?
Create a room for both of them (saved to DB)
Saved to database everytime they send a message to each other
Query the database when they login for list of the rooms the user has joined so that they could see the messages they have
That would probably be the best way to do it. Here is a similar question is there a good way to save socket.io message history it shows how to save the message history.
You do not have to save the messages, but if you would like to be able to see messages later, you should save every message to a database. If you do not save every message to a database, when you reload the page all previous messages will disappear.
If you want a free database, mongoLab will host small mongoose databases for free. Here is a good tutorial showing how to use mongoLab, it does not use socket.io, but it will probably be very easy to implement it.
Hope this helps! Feel free to ask any questions below.

Telegram Bot Event When Users Join To Channel

After create telegram bot, access and admin this bot to channel. how to get channel members list or event when users join to this channel?
Pretty disappointed with the current answers, so I'll leave an updated (as of February 2018) answer that explains how to do this with the Telegram API itself, as well as with the framework I am using, Telegraf for Node.
The Telegram API is both very powerful, and pretty simple as far as API's go. If you are using the polling method of getting updates, and not websockets which are a whole other issue, checking if someone new has been added to a group or channel is very easy.
The API method getUpdates returns an array of Update objects, which contain all of the possible information you could want including any messages sent, inline queries, and new chat members. To get any new chat members you simply need to access update.message.new_chat_members which will contain an array of new users. For reference you can look in the API documentation here.
To fetch the update objects in browser, or with curl, all you have to do is send a GET or POST request to https://api.telegram.org/botYOUR-BOT-TOKEN/getUpdates. Then just look for messages->new_chat_members.
If you are using the Telegraf bot framework with NodeJs you can use the bot.on method with the event new_chat_members.
Example:
bot.on('new_chat_members', (ctx) => console.log(ctx.message.new_chat_members))
I know this was asked a while ago, but I hope this helps other people searching.
From docs:
new_chat_members New members that were added to the group or supergroup and information about them (the bot itself may be one of these members)
So i think you can't.
To get updates on users join group or channel you must correctly setup webhook for your bot. This update type is disabled by default. See https://core.telegram.org/bots/api#setwebhook
allowed_updates
A JSON-serialized list of the update types you want your bot to
receive. For example, specify [“message”, “edited_channel_post”,
“callback_query”] to only receive updates of these types. See Update
for a complete list of available update types. Specify an empty list
to receive all update types except chat_member (default). If not
specified, the previous setting will be used.
Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received
for a short period of time.
So, the request should look like this:
https://api.telegram.org/<BOT_TOKEN>/setWebhook?url=<WEBHOOK_URL>&allowed_updates=["chat_member"]
Yes! You can use this:
https://api.telegram.org/bot[TOKEN]/promoteChatMember?chat_id=#[channelname]&user_id=[user_id]
if user is joined response is ok, else response is not ok.

Resources