I'm trying make a chrome extension which shows a notification when I get a message from socket.io node.js server.
Can I firstly say I have looked around a lot; some people have had similar errors but I can't seem to understand the answers and how to correct them on my PC because mine may have a different layout. Talking individually like this is better in my opinion.
This is what I have in my directory extension entitled "Projects": My extension directory
In content.js : Uncaught ReferenceError: io is not defined
var socket = io.connect('http://localhost:1337');
socket.on("hello",function(data){
console.log(data.text);
chrome.runtime.sendMessage({msg:"socket",text:data.text},function(response){});
});
In manifest.json :
{
"background": {
"scripts": [ "background.js" , "socket.io.js"]
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": ["http://*/*", "https://*/*"]
} ],
"manifest_version": 2,
"version":"1",
"name": "MyExtension"
}
In background.js :
chrome.runtime.onMessage.addListener(
function(request,sender,senderResponse){
if(request.msg==="socket"){
console.log("receive from socket server: "+request.text);
}
}
);
And finally, app.js, my server :
var app = require('http').createServer(handler).listen(1337);
var io = require('socket.io').listen(app);
function handler(req,res){
console.log(req.url);
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello Node\n You are really really awesome!');
}
io.sockets.on('connection',function(socket){
socket.emit('hello',{text:"node!"});
});
This is what is inside node_modules. NOTE : THE ONLY NPM I HAVE INSTALLED IS SOCKET.IO Inside node_modules