Bridge.IO 3.x
Bridge.IO is a realtime websocket framework for NodeJS.
Features
- Private messaging
- Broadcasting
- Channel support (Pub/Sub)
- Acknowledgement callback (RPC - Request/Response)
- Auto-reconnection
- Disconnection detector
- Built-in ping-pong mechanism
- Authentication
Installation
npm i bridge.io
Basic Usage
Server-Side
const app = require('express')();
const server = require('http').Server(app);
const BridgeIO = require('bridge.io');
const io = new BridgeIO(app, server, {
noServer: true
});
io.on('connection', (socket, request) => {
console.log('A user connected');
socket.on('sum', (io, socket, data, response) => {
const { a, b } = data;
return response(a + b);
return response({
status: 200,
data: a + b
});
return response({
status: 400,
data: 'Error'
});
});
socket.on('disconnected', () => {
console.log('User disconnected!');
});
});
server.listen(3000, () => {
console.log(`Server started on port ${server.address().port} :)`);
});
Client-Side
https://github.com/hossein-zare/bridge.io-client
<script src="/bridge.io/bridge.io.js"></script>
<script>
const socket = new BridgeIO({
server: 'ws://localhost:3000',
protocols: [],
response_timeout: 5000,
attempts: null,
delay: 2000,
reconnection: true
});
socket.on('open', (e, reconnected) => {
console.log('Connected');
});
socket.on('connection', (e, reconnected) => {
console.log('The connection is ready!');
const config = { timeout: 3000 };
socket.cast('sum', { a: 1, b: 2 }, function response(result) {
console.log('Result:', result);
}, function error(e) {
if (e === null) {
} else {
console.log(e.status, e.data);
}
}, config);
});
socket.on('disconnected', (e) => {
console.log('disconnected');
});
socket.on('reconnecting', (e) => {
console.log('reconnecting');
});
socket.on('reconnection', (e) => {
console.log('reconnected');
});
socket.on('message', (data) => {
console.log(data);
});
socket.on('error', (e) => {
console.log('error');
});
</script>
Authentication
Server-Side
const url = require('url');
io.auth(async (socket, request) => {
const token = url.parse(request.url, true).query.token;
const data = await authentication(token);
if (data.isAuthenticated) {
socket.user = data.user;
}
return data.isAuthenticated;
});
Client-Side
const token = 'myToken';
const socket = new BridgeIO(`ws://localhost:3000/?token=${token}`);
Methods & Properties
IO
io.to(id: string).cast(event: string, data: string|object|boolean|number);
io.broadcast(event: string, data: string|object|boolean|number);
io.channel(channel: string|array, except: array).cast(event: string, data: string|object|boolean|number);
io.clients.all();
io.clients.get(id: string);
io.clients.has(id: string);
io.channels.all();
io.channels.get(channel: string);
io.channels.has(channel: string);
io.channels.subscribers(channel: string|array);
Socket
socket.cast(event: string, data: string|object|boolean|number);
socket.cast(event: string, data: string|object|boolean|number, callback: () => void);
socket.broadcast(event: string, data: string|object|boolean|number);
socket.channel(channel: string|array, except: array).cast(event: string, data: string|object|boolean|number);
socket.subscribe(channel: string);
socket.unsubscribe(channel: string);
socket.close(code: Number, reason: string);
const id = socket.id;
const channels = socket.channels;