Web push notification - Do I need to own a server? - javascript

I have achieved a simple push notification using source code from here: [https://itnext.io/an-introduction-to-web-push-notifications-a701783917ce][1]
But I want to do more advance by sending push notification remotely to another computer using Chrome like Facebook and some others do.
Now I am able to register subscription and get the endpoint link like this:
{
"endpoint": "https://fcm.googleapis.com/fcm/send/cThXDSA40Mo:APA91bE7lTJvxlF52GsX6huXPogc7C-nAcCrX6r9WrMSAwWToz7UDexDnLnAJJdoG-FSbQfquR8bjpd5Oc_CZQYTHU12gF_7_YmdK06CV77uXhSB6M-6LkmdjMazU5MQisXgjyUEICEd",
"keys": {
"p256dh":
"BGaCEBZSsJoqLU_l1hFWpFRwYMH8dHPkfPrPfyO2Sev_ilBYfFwJ7w4W-ppwKmxdhNTcACbi3sf7f0qerSNmQBI",
"auth":"FPssMOQPmLmXWmdSTdbKVw=="
}
}
But I donĀ“t know how to go forward with this. Still wonder if I need to own a server or not ?

It helps if you have a server, so you can see every step of the process and understand what's happening, but if you strictly want to test your app you could use this companion site to send a notification.

Related

Role-based chat system in Nodejs

I've implemented a chat feature using socket.io and nodejs. But now I want to implement a role based chat application. For example, if someone wants to communicate with someone who is in a higher position, first needs to send a request. I've implemented a chat request system(similar to friend request). But I dont know how to make it role-based.
Can accesscontrol package be helpful in this case?
I've added a role property into the user model. I wanted to create a middleware to check what role has the user and to whom it wants to send the message. if its allowed it can continue with the 'chat', but if its not allowed I want it to redirect it to the request endpoint. I wanted to know if this logic is ok. If not, what could I do better.
EDIT: This solution with the middleware it seems to work :). If you have any better idea please answer my question
I don't have enough information to know exactly your case but it doesn't sound like you need access control. Here's my suggestion for you:
you need to store a role for each user.
when someone sends a message for the first time, you check if they can do it, doing something similar to this:
const senderRole = 'normal', receiverRole = 'extreme'; // YOU NEED TO FETCH THESE FROM YOUR DB
const roles = ['normal', 'better', 'extreme', 'best'];
if (roles.indexOf(senderRole) < roles.indexOf(receiverRole)) {
// need to ask for request
}
when they need to ask for request you would open a chat with the receiver but set in your database a special setting on the chat model: (access=false)
when the receiver goes in the app, they have a special section for chats filtered with the option (access=false), when they approve the chat, you change the setting and enable the chat to go back into the main queue and display messages

How to notify without websocket

I want to integrate a simple notification system in my react application. I want to notify for example:
- new post (when the user post the system need time to transcode the media attached and the publication)
- missing settings (the user need to compile some information)
- interesting posts etc..
There is a simple way to add a websocket, like socket.io, to a reactjs app with an aws lambda backend?
All the notification not need to be read in real time, maybe an ajax call every 2 minutes can solve my problem, but, in this case, someone can help me avoid ajax call if the app isn't used(like if the app remain opened in a foreground tab...)
componentDidMount() {
this.liveUpdate()
setInterval(this.liveUpdate, 120000);
}
liveUpdate() {
axios.get(endpoint.posts+'/live/', cfg)
.then(res => {
// ...
});
}
This code is in the footer component, the call happen every 120 seconds, but the call will still happen also if a user leave the application opened in the browser and not use it, this on a lambda backend mean a waste of money.
There are 3 main ways of notifying that I can think of at the moment...
Long polling (using ajax etc)
Websocket
Push Notification
Push (though) requires permission from the user

node.js - how to push an address change to a client?

I am trying to set up a simple presentation using three computers synchronized by a central server, and I figured node would be the ideal tool.
I was wondering if there's any way to have all three computers connect to the server via the browser, and if I could control the server to push changes to each?
For example:
Computer-1 visits 10.0.0.1?comp=1?slide=1
Computer-2 visits 10.0.0.1?comp=2?slide=1
Computer-3 visits 10.0.0.1?comp=3?slide=1
Then from the server commandline, I would like to be able to trigger a change so the clients will each be redirected accordingly like so:
Computer-1 visits 10.0.0.1?comp=1?slide=2
Computer-2 visits 10.0.0.1?comp=2?slide=2
Computer-3 visits 10.0.0.1?comp=3?slide=2
I'm new to node, so I'm not even sure if this is the ideal platform, but was wondering what terminology I should be researching to be able to build something like this?
Thank you for your responses, I ended up looking into socket.io and managed to write this system in one evening! Node + socket.io and express is a pretty amazing tool with the socket emit events.
Thank you for pointing me in the right direction, this is exactly the tool I was looking for.
Just in case it may help anyone, in my client/jade template, I have something like:
socket.on('slideUpdate', function (data) {
// Do things with the data
}
and on the server app.js:
io.on('connection', function (socket) {
socket.on('slideChange', function (data) {
// logic for setting slide data
io.sockets.emit('slideUpdate', { example: exampleData ... });
});
});
where a slideChange event is triggered via a button on the client-side template.
For such a presentation I would use the most simple solution, i.e. not websockets, not server-sent events and not long-polling.
Just do a short poll, i.e. every client calls the server every 100ms for updates. The server then responds with a status update (if there is one).

How do I send a channel message using channel.trigger with websocket-rails gem

I'm building a simple real-time chat app to learn how to use websockets with RoR and I don't think I'm understanding how channels work because they're not doing what I expect. I can successfully send a message to my Rails app using the dispatcher.trigger() method, and use my websocket controller to broadcast a message to all clients that subscribe to the channel. That all works fine. What does NOT work is using a channel (via the channel.trigger() method) to send a message to other clients. The websocket-rails wiki says...
Channel events currently happen outside of the Event Router flow. They
are meant for broadcasting events to a group of connected clients
simultaneously. If you wish to handle events with actions on the
server, trigger the event on the main dispatcher and specify which
controller action should handle it using the Event Router.
If I understand this correctly, I should be able to user the channel.trigger() method to broadcast a message to clients connected to the channel, without the message being routed through my RoR app, but it should still reach the other connected clients. So here's my code...
var dispatcher = new WebSocketRails('localhost:3000/websocket');
var channel = dispatcher.subscribe('channel_name');
channel.bind('channel_message', function(data) {
alert(data.message);
});
$("#send_message_button").click(function() {
obj = {message: "test"};
channel.trigger('channel_message', obj);
});
With the code listed above, I would expect that when I click the button, it sends a channel message using channel.trigger() and the channel_message binding should be executed on all clients, displaying an alert that reads "test". That doesn't happen. I'm using Chrome tools to inspect the websocket traffic and it shows the message being sent...
["channel_message",{"id":113458,"channel":'channel_name',"data":{"message":"test"},"token":"96fd4f51-6321-4309-941f-38110635f86f"}]
...but no message is received. My questions are...
Am I misunderstanding how channel-based websockets work with the websocket-rails gem?
If not, what am I doing wrong?
Thanks in advance for all your wisdom!
I was able to reproduce a working copy based on an off-the-shelf solution from the wiki along with your very own code.
I've packaged the whole thing here. The files you might be interested are home_controller.rb, application.js and home/index.html.erb.
It seems your understanding of channel-based websockets is correct. About the code, make sure to load the websocket javascript files and to enclose your code inside a document.ready. I had the exact same problem you're having without the latter.
//= require websocket_rails/main
$(function() {
// your code here...
});
Let me know if it works. Best Luck!

Facebook Feed Dialog: Post not showing up on friend's wall even though I get a response id

I am creating an app on facebook and I am trying to post to a friend's wall. I'm using the facebook javascript SDK and the FB.ui method to do this. In short here is my code:
function test() {
var obj = {
method: 'feed',
to: '######'
};
function callback(response) {
alert(response['post_id']);
}
FB.ui(obj, callback);
}
Note that I made this code very simple for testing purposes.
When I run it, a facebook feed dialog opens correctly and at the top says "Post Story to friendsname's Wall". I type in a message and press Share. My alert pops up with a response['post_id'] number. Because there is a response['post_id'] the story should've been posted successfully right? However when I navigate to the friend's wall there is no story. I've tried this multiple times in slightly different ways and haven't been able to get it to work. If I remove the 'to' parameter and simply post to my own wall it -does- work. So for some reason, posting to a friend's wall breaks it?
I know it's a rather broad question but I was wondering if anybody had any ideas why this doesn't work. Thanks
I suspect the problem may be because your application is in sandbox mode - https://developers.facebook.com/docs/ApplicationSecurity/
When testing your apps, place them into Sandbox Mode. This hides your
app entirely from all users who you have not authorized in the
developer app to see the app, for the roles described below. Please
note that when your app is in Sandbox Mode, you cannot call any API
calls on behalf of users who cannot see your app.
Basically, if your application is in sandbox mode, nothing your application does can be seen by users who have not authorized your application or by users that have not been specifically listed in the "roles" within your applications settings.
Remove you application from sandbox mode or add some users to specific "roles" within your application.

Categories

Resources