connect.io

Real-time bidirectional event-based and Promise friendly communication in Chrome extensions or Apps inspired by Socket.IO.
Install
npm i -S connect.io
Usage
background.js:
const server = new ChromeConnect.Server('optionsal namespace, default is "default"');
server.on('connect',(client)=> {
if (client.exteranl && client.port.sender.url === YourBlackList) {
client.disconnect();
return;
}
client
.send('welcome','hello world',true)
.then(
response => console.log(response),
error => console.log(error)
);
client.send('send true and do not need response',true,false);
client.send('use 1 to instead of true',1);
client.broadcast('join','new client joined.');
client.on('report clients number', (data, resolve, reject) => {
resolve(server.ports.length);
});
client.once('disconnect', isOtherSide => {
server.send(isOtherSide ? 'Someone out by himself.' : 'I knock it out.');
});
});
content-scripts.js:
ChromeConnect.send({
eId:'',
tabId:23,
frameId:0,
namespace:'default',
name:'your msg name',
data:{ your:'data' },
needResponse:true
}).then(
);
const client = new ChromeConnect.Client('optional extensions or apps id. default value is chrome.runtime.id',{
frameId:0,
namespace:''
});
client.on('welcome',(data,resolve,reject) => {
console.log(data);
resolve('Thanks!');
reject('I\'m not happy.');
});
client.on('join',function(data){
console.log(data);
});
client
.send('report clients number',true)
.then(
res => console.log(res) ,
error => console.log(error)
);
client.on('Someone out',()=> {
});
client.once('disconnect', isOtherSide => {
console.log('Connection disconnected by ', isOtherSide ? 'the other side' : 'myself', '.');
});
client.disconnect();