queued-socket.io
Socket.io client with offline queue
Installation
Install using npm:
npm install queued-socket.io
Basic Usage
const socket = require('queued-socket.io');
socket.on('ping', () => console.log('ping'));
socket.on('disconnect', () => console.log('disconnected'));
socket.once('ping', () => console.log('One time ping'));
socket.emit('authentication', { token: 'loginToken' }, 1);
const options = {
path: '/socket',
transports: ['websocket']
};
const client = socket.connect('http://localhost:5000', options);
console.log(`Socket is ${socket.isConnected() ? 'connected' : 'disconnected'}`);
API Reference
Socket module
socket~getClient() ⇒
Retrieve the client
Kind: inner method of socket
Returns: socket.io client
Access: public
socket~isConnected() ⇒ Boolean
Check if the socket is connected
Kind: inner method of socket
Returns: Boolean
- socket connection status
Access: public
socket~emit(event, data, priority)
Send an event to the socket, when the socket is not connected, add an emit event to the queue.
Kind: inner method of socket
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
data | Object | | The data to send to the socket when the event is triggered. |
priority | Number | 2 | The priority of the event. |
socket~on(event, callback, priority)
Wrapper around socket.on that adds priority and caching
Kind: inner method of socket
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
callback | function | | The callback function that the sockets call when the event is triggered. |
priority | Number | 2 | The priority of the event. |
socket~off(event, priority)
Wrapper around socket.off that adds priority and caching
Kind: inner method of socket
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
priority | Number | 2 | The priority of the event. |
socket~once(event, callback, priority) ⇒
Wrapper around socket.once that adds priority and caching
Kind: inner method of socket
Returns: socket.io client
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
callback | function | | The callback function that the sockets call when the event is triggered. |
priority | Number | 2 | The priority of the event. |
socket~connect(uri, options) ⇒
Connect to socket.io, if socket is already connected, returns that socket.
Kind: inner method of socket
Returns: socket.io client
Access: public
Param | Type | Description |
---|
uri | String | The connection uri of the host. |
options | Object | Options object used by socket.io. |
Events module
events~add(event, callback, priority)
Adds a socket event to the socket, when the socket is not connected, add the add event to the queue.
Kind: inner method of events
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
callback | function | | The callback function that the sockets call when the event is triggered. |
priority | Number | 2 | The priority of the event. |
events~once(event, callback, priority)
Adds a single run socket event to the socket, when the socket is not connected, add the once event to the queue.
Kind: inner method of events
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
callback | function | | The callback function that the sockets call when the event is triggered. |
priority | Number | 2 | The priority of the event. |
events~clear(priority)
Remove all socket events from the socket, when the socket is not connected, add the clear event to the queue.
Kind: inner method of events
Access: public
Param | Type | Default | Description |
---|
priority | Number | 2 | The priority of the event. |
events~get() ⇒ Array.<Object>
Retrieve all registered events
Kind: inner method of events
Returns: Array.<Object>
- Registered events
Access: public
events~remove(event, priority)
Remove a socket event from the socket, when the socket is not connected, add a remove event to the queue.
Kind: inner method of events
Access: public
Param | Type | Default | Description |
---|
event | String | | The event name that needs to be added to the socket. |
priority | Number | 2 | The priority of the event. |
Debug/logging
queued-socket.io makes use of debug.
To see the output in the console run this in the console:
localStorage.debug = 'queued-socket.io*';
Contributing
Please submit all issues and pull requests to the anchorchat/queued-socket.io repository!
Tests
Run tests using npm test
.
Releasing
Make sure your working directory is clean!
git checkout master && git fetch && git merge origin/master
npm run prepare
npm version <patch|minor|major> -m '<commit message containing changes for this release>'
git push origin master
git push --tags
npm publish
Support
If you have any problem or suggestion please open an issue here.