Giter VIP home page Giter VIP logo

evsocket-client's Introduction

EvSocket-Client

Why use this?

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.

About

Client module for evsocket. Abstracted WebSocket module with event driven interface and rich functionalities. See full example here.

Features

  • Event Driven Interface
  • Easy Authentication
  • Binary Transmission
  • Middlewares
  • Channel system(like Room in Socket.io)
  • Socket Broadcasting
  • And more...!

Updates

  • [1.0.212] Fixed that getting Blob instead of ArrayBuffer when binary data received.

API

constructor EvSocket(string uri, object options)

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);
});

EvSocket.prototype.close(void)

Close WebSocket.

var socket = new EvSocket('ws://localhost:3000');
socket.on('open', function() {
	console.log('Connected as ' + socket.id);
	socket.close();
});

EvSocket.prototype.send(string evName, object data)

Send the data with event name to client.

var socket = new EvSocket('ws://localhost:3000');
...
socket.send('some-server-event', { a: 1, b: 2});

EvSocket.prototype.sendBinary(string evName, ArrayBuffer data)

EvSocket.prototype.sendBinary(string evName, TypedArray data)

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]));

EvSocket.prototype.on(string evName, function fn)

Add event listener.

EvSocket.prototype.once(string evName, function fn)

Add event listener that execute only once and delete itself automatically.

EvSocket.prototype.off(string evName[, function fn])

Remove event listener. If fn argument is undefined, remove all listeners in specified event name.

EvSocket.prototype.emit(string evName[, object data])

Fires event of server socket.

EvSocket.prototype.join(string channelName)

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');

EvSocket.prototype.leave(void)

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');

EvSocket.prototype.broadcast(string evName[, object data])

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}.`);
});

Events

You can use any name of the event, except these default events. These default events are triggering by EvSocket directly.

onopen

Fired on EvSocket is ready.

onauthenticated

Fired on EvSocket passed authentication.

onunauthorized

Fired on EvSocket failed authentication. After this event occurs, socket will close.

onclose

Fired on socket closed. Arguments are code and reason, please check this: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes

onerror

Fired on error occured. Has one argument, error object.

onchanneljoin

Fired on user joined channel.

onchannelleave

Fired on user left channel.

evsocket-client's People

Contributors

rico345100 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.