The session is supported by egg-socket.io. It’s behaviour just like the normal HTTP session.
Session creates or check just happens at the handshake period. Session can be accessed by ctx.session in packetMiddleware and controller, but it’s only created at the handshake period.
The feature is powered by egg-session, make sure it has been enabled.
Cluster
If your Socket.IO service is powered by mutil server, you must think about cluster solution. It can’t work without cluster like broadcast, rooms and so on.
It’s very easy to implement sharing source and event dispatch with socket.io-redis built in.
config at config/config.${env}.js :
exports.io = {
redis: {
host: { redis server host },
port: { redis server prot },
auth_pass: { redis server password },
db: 0,
}
};
Application will try to connect the redis server when booting.
egg-socket.io
egg plugin for socket.io
Install
Requirements
Configuration
Change
${app_root}/config/plugin.jsto enable Socket.IO plugin:Configure Socket.IO in
${app_root}/config/config.default.js:uws
Egg’s socket is using
ws, uws is deprecated due to some reasons.If you insist using this, please config like this following:
init: engine.io.Egg Socketin default : config.default.js.generateId
Note: This function is left on purpose to override and generate a unique ID according to your own rule:
Deployment
Node Conf
Because of socket.io’s design, the multi process socket.io server must work at
stickymode.So, you must start cluster server with
stickyset to true, otherwise it will cause handshake exception.which will start egg cluster with:
Nginx Conf
if you use a nginx proxy server:
Usage
Directory Structure
Middleware
middleware are functions which every connection or packet will be processed by.
Connection Middleware
app/io/middleware/auth.jsconfig/config.default.jspay attention to the namespace, the config will only work for a specific namespace.
Packet Middleware
app/io/middleware/filter.jsconfig/config.default.jspay attention to the namespace, the config will only work for a specific namespace.
Controller
controller is designed to handle the
emitevent from the client.example:
app/io/controller/chat.jsnext, config the router at
app/router.jsRouter
A router is mainly responsible for distributing different events corresponding to a controller on a specific socket connection.
It should be configured at
app/router.jsrefer to the last chapter.Besides that, there are several system Event:
disconnectingdoing the disconnectdisconnectconnection has disconnectederrorError occuredExample:
app/router.jsapp/io/controller/chat.jsSession
The session is supported by
egg-socket.io. It’s behaviour just like the normal HTTP session.Session creates or check just happens at the handshake period. Session can be accessed by
ctx.sessionin packetMiddleware and controller, but it’s only created at the handshake period.The feature is powered by egg-session, make sure it has been enabled.
Cluster
If your Socket.IO service is powered by mutil server, you must think about cluster solution. It can’t work without cluster like broadcast, rooms and so on.
It’s very easy to implement sharing source and event dispatch with socket.io-redis built in.
config at
config/config.${env}.js:Application will try to connect the redis server when booting.
Questions & Suggestions
Please open an issue here.
License
MIT