connect.io
Real-time bidirectional event-based and Promise friendly communication in Chrome extensions/apps inspired by Socket.IO.
Install
Use with Webpack
If you build your project with Webpack, you can install connect.io via npm:
npm install connect.io
Then you can import it in your project:
// es6 // commonjsconst createClient createServer send =
Use with <script>
Download chrome-connect.js from unpkg(min version).
Then reference it in your html:
<!-- now you will get a global variable named chromeConnect -->
Usage
Send message from content script to background
First, create a server in background page(or other pages like popup, options):
const server =
Second, listen connect
event on server:
server
Finally, create a client in content script:
// client is an Client objectconst client =
For more information about the Client
object please read the API below.
Send message from background to content script
Only different with the above steps is: when you create client in background, you must specify the tab id in createClient
.
For example, in your background:
const clientInBackground = // the tab id you want to connect
Using namespace
Server can optional has a namespace:
const serverTweets = serverTweets const serverNotification = serverNotification
You can connect to different server in client:
const clientTweets = clientTweets const clientNotification = clientNotification
Get response from server
You can send response from server to the client. For example:
// in server const server = server
// in client const client = // pass true as the last params to get responseclient client
Send one-time message
If you want to send one-time message and don't want to create a client, you can use send
method:
API
createServer([namespace])
Return a Server
object.
Note: same namespace get the same Server
object.
=== // true === // true === // false
Server
Create from createServer
method.
Server#send(name[, data])
Send message to all clients that connect to this server.
Server#on(event, handler)
Listen event. For now there is only one event: connect
.
createClient([id, options])
Return a Client
object.
If you want to connect to content script from background, then id
is necessary and must be a tab id.
options
has these property:
namespace
: which server you want connectframeId
: if you are connect to content script from background, you can specify a frameId to connect only this frame.
Client
Create from createClient
, or provided in server.on('connect')
event.
Client#port
The native chrome Port object.
Client#external
If this client is connect from webpage or other extension, then this property is true
.
Client#send(name[, data, needResponse])
Send message to server or client.
Client#on(name, handler(data, resolve, reject))
Receive message from server or client. Use resolve
or reject
method to response this message to server or client.
Client#broadcast(name[, data])
Note: this method only exist in server.
Sending a message to everyone else except for the connection that starts it.
Client#disconnect()
Disconnect the connection.
send(options)
Send one-time message.
The options
has these property:
id
: optional. If you want to connect to content script from background, thenid
is necessary and must be a tab id.frameId
: optional. SeecreateClient([id, options])
API.namespace
: optional. SeecreateClient([id, options])
API.name
: the message name.data
: the data you want send.needResponse
: if you need response, then set this totrue
License
MIT