In my application NodeJs is running on xxx port. Mouse click event trigger node js function using socket.io. My application have real time notification feature.
My Question is
Can I trigger nodeJs method when real time notification comes from google
because, method which handle this notification not load any view ?
For pushing server to node you can use redis for that. you can use a code like below. so for that you need to install redis. you can find installtaion steps on google.
At your server side for e.g. you are getting the data into your function.
$redisobject = new Redis();
$redisobject->connect('127.0.0.1');
$purchase_info = json_encode(array('user_id' =>'2','purchase_information'=>'sample_data')); // json array of data which you want to post
$redisobject->publish('redis_data_php', $purchase_info); //your channel name and data.
Inside your node.js after installing redis module using npm
var redis = require('redis');
var redis_client = redis.createClient();
redis_client.subscribe('redis_data_php'); // same channel name as you have mentioned on php side
redis_client.on('message', function(channel, message){
var purchase_data = JSON.parse(message);
console.log(purchase_data);
socket.emit('redis_data', {"info" : purchase_data.purchase_information}); //emit your data to client as you want.
});
basically redis will create a channel between php and node.js so you can communicate via that channel.
Need to install below things
Install redis (if you are using ubuntu than follow this link https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis)
Than install redis for php (https://joshtronic.com/2014/05/12/how-to-install-phpredis-on-ubuntu-1404-lts/)
Install redis for node (npm install redis)
Hope this will work for you...
Related
I have a broadcast server with socket.io and ioredis via node.
However, with my current form I can only subscribe to one channel a time.
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('mychannel');
Considering that I must have innumerable channels (one for each registered user, for instance) I cannot hard type every channel on the node server.
I've tried also redis.subscribe('*') but without success.
Any light?
Using redis.psubscribe('*') and redis.on('pmessage', handlerFunction) did the trick.
i am trying to include chat features on my MEAN app, all i have completed till now is a medium where all connected users can communicate.not a separate group. i follow some of the tutorials but they do by trick like sending some key words in front of message(whistle as they say).
as far as i know every connected users are provided a seperate socket ID through which communication is carried but i failed getting that id.
module.exports = function(socket){
//console.log(socket);
var users =[];
socket.on('username',function(data){
users.push({id:socket.id,username:data.message});
socket.emit('username',users)
})
console.log('connected');
socket.on('typing',function(data){
//socket.emit('typing',{message:"helo angular"});
socket.broadcast.emit('typing',{message:data.message});
});
It shows me socket is not defined, any one has better idea how to perform private message using socket.io and node.js
can anyone enlighten me about this.
Make all users join their own unique group.
socket.join(socket.id);
Then you can send a message to one user by doing this
io.sockets.in(whichever_user.id).emit('msg', "Hello there user lets have a private chat!");
I would recommend using the Express framework of Node, and implenting Socket.io into it.
Here are some links telling you how to set up Express and Socket.io:
https://www.youtube.com/watch?v=WH5qsGnFkBM&lc=z135dbryaqbfitiee22pd33ouozhwft3q
---Youtube comment tells you how to set up express generator.
Adrian GÄ…siewicz:
For those who want to have similar directory structure as Bucky:
Install Express generator "sudo npm install -g express-generator"
Go to directory where you want to create project and type "express myapp --ejs" 3. Select newly created directory --> "cd myapp"
Install all node_modules --> "npm install"
Run server --> "npm start"
Open browser and type "http://localhost:3000/" Have fun ;)
Using socket.io in Express 4 and express-generator's /bin/www
---This tells you how to set up socket in Node.js Express project.
io.sockets.clients(room).length was working fine in socket.io version 0.9.16 & 0.9.17 .
But seems to be its not working in socket.io version 1.0.4.
Getting an error like "TypeError: Object # has no method 'clients'"
Can any one tell me what is the change need to work with new socket.io ?
From this previous question : Listing all the clients connected to a room in Socket.io version > 1
To get socket IDs of the clients connected to a room use this code:
var namespace = '/';
var roomName = 'my_room_name';
for (var socketId in io.nsps[namespace].adapter.rooms[roomName]) {
console.log(socketId);
}
io.sockets.clients(room).length function is almost not necessary at all
you can just use javascript code to build a list of your clients
At the Server-side go ahead and create a new event
var users={};
socket.on('addUser',function(username){
socket.username=username;
users[username]=username;
io.sockets.emit('updateUsers',users);
});
then at the client-side javascript file emit the usernames
function addUser(){
socket.emit('addUser',prompt('What\'s your name'));
}
I am dealing with a sort of gps tracking system. i have a map and i need to update the current position of the vehicle. i need to use socket io for real time visualization. i checked out nodejs tutorials but could not derive a solution. i have a server written in java and i want to send updates to my map written in javascript. so how can i receive data from my java server by using socket io and/or nodejs?var http = require("http");
var server = http.createServer();
server.listen(????) what should i add to these code?
You could use WebSockets (HTML5). Install the websocket package to the folder where your node.js server is running:
npm install websocket
Here a link to a good tutorial about websockets and node.js.
I am unsure about where would be the best place to define the redis client in a Express application I am building. I am using skeleton as the framework.
It seems like the connection to redis should go in either boot.coffee or app.coffee, but then I can't easily get a reference to it in application_controller.coffee, which is where I need it.
If I put client = redis.createClient in application_controller.coffee, will that mean a new client is created on each request?
I would define the Redis client in app.coffee (after configuration, before the routes) and set the Redis client as a property on the App object:
app.client = redis.createClient
Then in application_controller.coffee you can access the Redis client by app.client.