There is well-known alternative for this module, Socket.io, but unfortunately Socket.io doesn't support React Native, because it uses Node.js features inside. This module built for provide highly abstracted WebSocket interface without any Node.js related features, only used pure JavaScript features. When I was used WebSocket with vanila JS, it was really hard to make something rich. EvSocket provides similar interface like Socket.io with only uses WebSocket + JavaScript features so it is safe to use any WebSocket supporting JavaScript platform.
Client module for evsocket. Abstracted WebSocket module with event driven interface and rich functionalities. See full example here.
- Event Driven Interface
- Easy Authentication
- Binary Transmission
- Middlewares
- Channel system(like Room in Socket.io)
- Socket Broadcasting
- And more...!
- [1.0.212] Fixed that getting Blob instead of ArrayBuffer when binary data received.
Create new EvSocket. Available options are:
- string protocol: Set protocol
var socket = new EvSocket('ws://localhost:3000');
socket.on('open', function() {
console.log('Connected as ' + socket.id);
});
Close WebSocket.
var socket = new EvSocket('ws://localhost:3000');
socket.on('open', function() {
console.log('Connected as ' + socket.id);
socket.close();
});
Send the data with event name to client.
var socket = new EvSocket('ws://localhost:3000');
...
socket.send('some-server-event', { a: 1, b: 2});
Send the binary data with event name to client.
var socket = new EvSocket('ws://localhost:3000');
...
socket.sendBinary('some-server-event', new Uint8Array([1,2,3,4,5,6,7,8]));
Add event listener.
Add event listener that execute only once and delete itself automatically.
Remove event listener. If fn argument is undefined, remove all listeners in specified event name.
Fires event of server socket.
Join the specified channel.
var socket = new EvSocket('ws://localhost:3000');
socket.on('channeljoin', (channelName) {
console.log(channelName);
socket.leave();
});
socket.on('channelleave', () => {
console.log('Channel left.');
});
socket.join('channel1');
Leave the current channel.
var socket = new EvSocket('ws://localhost:3000');
socket.on('channeljoin', (channelName) {
console.log(channelName);
socket.leave();
});
socket.on('channelleave', () => {
console.log('Channel left.');
});
socket.join('channel1');
Broadcast message to users who currently in same channel.
var socket = new EvSocket('ws://localhost:3000');
socket.on('open', () => {
socket.join('channel1');
});
socket.on('channeljoin', (channelName) => {
socket.broadcast('chat', `User ${socket.id} joined to ${channelName}.`);
});
You can use any name of the event, except these default events. These default events are triggering by EvSocket directly.
Fired on EvSocket is ready.
Fired on EvSocket passed authentication.
Fired on EvSocket failed authentication. After this event occurs, socket will close.
Fired on socket closed. Arguments are code and reason, please check this: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
Fired on error occured. Has one argument, error object.
Fired on user joined channel.
Fired on user left channel.