node-express-socket
v1.1.4
Published
A structure to add socket manager in express routes
Downloads
18
Maintainers
Readme
Express.js socket handler
This package is an easy way to implement and manage socket connections and also make a simple way to migrate from REST API to Socket API.
Installation instruction:
npm install node-express-socket
Use:
Important: It only can use by Router
and you can't use by express()
method like as express().socket
Server side:
const express = require('express');
const socket = require("node-express-socket");
const router = express.Router();
const app = express();
router.socket("/test1/:fname/:lname", (req, res) => {
res.send({m:req.method, q:req.query, b:req.body, p:req.params});
});
router.get("/test1/:fname/:lname", (req, res) => {
res.send({m:req.method, q:req.query, b:req.body, p:req.params});
});
const server = app.listen(8000, () => {
console.log(`Server running at port:8000`);
});
app.use(socket(app, server));
app.use(router);
If you want to use a general route for 'Not found', it should be after socket use command.
Get user socket id
Send message to specific user
Send broadcast message
Response socket message from out of socket methods
Client side:
// 1) Create and connect socket object
const socket = io();
// 2) Send request using WebSocket
socket.emit('/test1/joe/gandomi?qp1=v1', {
var1: "value1",
var2: "value2"
});
// 3) Receive response using WebSocket
socket.on('/test/*/*', data => {
console.log(data);
});
Client log:
{
m: "SOCKET",
q: {
qp1: "v1"
},
p: {
fname: "joe",
lname: "gandomi"
},
b: {
var1: "value1",
var2: "value2"
}
}