A set of generic events for sending and receiving json objects through socket.io.
Allows you to easily announce, broadcast and directly send data to other sockets, and also join/leave rooms through a basic client-side API.
$ npm install gsockets
var gsockets = require('gsockets');
// to enable debug logs, add this line
gsockets.config({ log: true});
// express middleware to host the client.js file at /gsockets/client.js
app.use(gsockets.client);
Using existing Socket.IO
io.on('connect', function(socket){
gsockets.bind(io, socket);
});
Using standalone mode
// choose whatever port number you want to use
gsockets.listen(1234);
<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
<script src="/gsockets/client.js"></script>
<script>
gsockets.connect();
gsockets.broadcast('user_connected');
gsockets.on('user_connected', function(){
console.log('A user has connected');
});
</script>
Serve the client.js file at the url /gsockets/client.js
- usage:
app.use(gsockets.client);
Modify configuration settings of gsockets. The only option currently available is log.
- usage:
gsockets.config({ log: true});
Add the gsockets events to your socket.io server
Create a separate instance of socket.io especially to run gsockets.
Create the connection to the socket.io server
- ip (optional) The ip of the server if different from the current page
- port (optional) The port of the server
The orginal socket.io object.
- usage:
var socketID = gsockets.socket.id;
Run the callback when an event is emited from the server
- event The name of the event to listen for
- callback The function to run when this event is fired
Run the callback when a socket disconnects
- callback The function to run when this event is fired
Emit event to all sockets
- event The name of the event that will be sent to another client
- data (optional) Data object
Emit event to this socket
- event The name of the event that will be sent to another client
- data (optional) Data object
Emit event to specific socket
- socketID The socket id to send this data to
- event The name of the event that will be sent to another client
- data (optional) Data object
Emit event to every socket except this one
- event The name of the event that will be sent to another client
- data (optional) Data object
Emit event to room
- roomName The name of the room to emit to
- event The name of the event that will be sent to another client
- data (optional) Data object
Emit event to everyone in room except you
- roomName The name of the room to emit to
- event The name of the event that will be sent to another client
- data (optional) Data object
Join room and emit an event to this socket
- roomName The name of the room to join
- event The name of the event that will be sent to another client
Leave room and emit an event to this socket
- roomName The name of the room to leave
- event The name of the event that will be sent to another client