Socket.io - Only Emit to Certain Users? - socket.io

Forewarning, I am very new to Node.js and Socket.io and this probably has a simple answer.
I am using it in conjunction with a PHP app. The intention is to have real-time notifications.
If you take a scenario such as Facebook -- something with lots of users, but the notifications should only be sent out to certain sets of users, how do I target those users?
I've thought of setting something like:
socket.on('notification-[user_id]', function() {...} );
But that would be insane considering thousands of users.
It also seems to be insane to send notifications to thousands of users and have each user have to figure out whether it's for them or not.
Is there a way around this? What am I missing?

I'm new to them too, but i think you can do it like this:
In the client side you can just:
socket.on('notification',function(){...});
In the server side you can use :
io.sockets.connected[socketid].emit('nofification',data);
Hope it can help you~

Related

Get Slack channel where app is integrated

I might be missing something really simple but don't seem to find the solution.
I have created a quick and simple app which is meant to do some processing every morning and then send some logs to Slack.
I have been perfectly able to do so using Incoming Webhooks. Cool. The "problem" is that it requires me to set it up from the app settings (aka it obviously makes me define a specific channel for the Webhook so I can have the specific URL) and that's something I like a bit less.
I thought it'd be easier if I can just add/integrate the app on a channel using the Slack UI so I don't have to worry about having to know beforehand the channel(s) ID where the message has to go to and also any other user would be able to integrate in any other channel they consider.
I have integrated it on my testing channel (all good) and tested chat.postMessage (all good) but it still needs the channel (obviously). However, using conversations.list lists ALL channels and that's the opposite of what I'm looking for.
I need a way of getting just those channels where the app is integrated so I can post the message to those and only those.
Is this something that Slack doesn't allow or I'm just missing something very obvious here?
I hope it makes sense and someone can shed some light on this :)
You can use users.conversations method to get list conversations the calling user/bot may access.
https://api.slack.com/methods/users.conversations
Use Bot token to call the API.
Use type argument to search public, private, mpim & im conversations
https://api.slack.com/methods/users.conversations#arg_types
Mix and match channel types by providing a comma-separated list of any combination of public_channel, private_channel, mpim, im

Slack: Get notification when Bot is offline

I searched the www for a solution to a problem of mine without finding an answer.
I need to get notificated when my bot in slack goes offline, but I don't know how.
To get notificated when he logs in is cacke, because I can set him to send a message, but when he's off there is no option to send a message.
I prefer PHP, Javascript, but other languages are also fine.
Thank you very much for your effort
There are many options how to setup a way to monitor your bot and get notified when it is offline.
One option that has worked well for me is to use an external website to monitor your bot. That website will send requests to your bot every couple of minutes to see if is is still alive and will notify you if the request fails. You may need to extend your bot a bit, so that he can reply to these requests in a proper way.
There are many website that provide this service. The one I use is uptimerobot, which also happens to be free for up to 50 monitors.

Is running an ajax request every second bad?

I am currently trying to create a chat function and notifications function on my site that would get the current users logged in and notifications periodically from a mysql database and right now I have it set to do an ajax call every second to a php page which returns the users online and notifications back in a json object. I am just trying to make sure there isn't a better way of doing this that I can't think of. Also is doing an ajax call to my server so often bad? I would really appreciate any insight on this. Thanks in advance!
As long as you have a hand full of chatters: no. It is not the best design, but it will do the trick. If you are looking for scalable techniques look for Comet or Web Sockets.

Smart real-time with socket.io

I'm currently developing a PHP website and it has a posts+comments system. I also have a socket.io server to have real-time communication through comments to posts (just like facebook). Everything is fine now, except that I send a comment to all connected users. And I'm thinking that when I'll have like 10.000 users and one comments on a post, it sends that comment to all 10.000 users and maybe the connection will crash pretty soon this way.
I think I have to keep the posts a user is viewing. Maybe in PHP, when I show up some posts, I keep in the database their id and it will expire after a period, like an hour. In this case, when somebody comments on a post, I get the users who are seeing that post and send the comment only to them. Is there a cleaner way to accomplish that?
Socket.io has the concepts of Rooms
You could create a different room for each post and have the clients subscribe only to the relevants rooms (posts).

Live chat using AJAX

Hello everyone
I just wanted to know if I use AJAX for my chat engine, will it crash my server or it will work fine for a traffic of about 1500 people at a time ??
Also suggest me some way to make automatic ajax request to update the contents of chat box. How much delay between consecutive query should be set so that it doesn't cause my server to overload ???
Thanks in advance :) :)
Depending on your requirements, you might want to look into WebSockets. At a local hacknight, we produced a simple chat system pretty quickly using http://faye.jcoglan.com/. It's a pub sub service that you can use javascript on the client side to subscribe to a specific channel. Then it's just a matter of coding the front end to work with the input and output.

Resources