question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RFC] egg-socket.io

See original GitHub issue

Directory Structure

app
├── io
│   ├── controller
│   │   └── chat.js
│   └── middleware
│       ├── auth.js
│       ├── filter.js
├── router.js
config
 ├── config.default.js
 └── plugin.js

Configuration

// {app_root}/config/config.default.js
exports.io = {
  namespace: {
    '/': {
      connectionMiddleware: [],
      packetMiddleware: [],
    },
  },
  redis: {
    host: '127.0.0.1',
    port: 6379
  }
};

Middleware

middleware are functions which every connection or packet will be processed by.

Connection Middleware

  • Write your connection middleware app/io/middleware/auth.js
module.exports = app => {
    return function* (next) {
        this.socket.emit('res', 'connected!');
        yield* next;
        // execute when disconnect.
        console.log('disconnection!');
    };
};
  • then config this middleware to make it works.

config/config.default.js

exports.io = {
  namespace: {
    '/': {
      connectionMiddleware: ['auth'],
    },
  },
};

pay attention to the namespace, the config will only work for a specific namespace.

Packet Middleware

  • Write your packet middleware app/io/middleware/filter.js
module.exports = app => {
    return function* (next) {
        this.socket.emit('res', 'packet received!');
        console.log('packet:', this.packet);
        yield* next;
    };
};
  • then config this middleware to make it works.

config/config.default.js

exports.io = {
  namespace: {
    '/': {
      packetMiddleware: ['filter'],
    },
  },
};

pay attention to the namespace, the config will only work for a specific namespace.

Controller

controller is designed to handle the emit event from the client.

example:

app/io/controller/chat.js

module.exports = app => {
  return function* () {
    const message = this.args[0];
    console.log(message);
    this.socket.emit('res', `Hi! I've got your message: ${message}`);
  };
};

next, config the router at app/router.js

module.exports = app => {
  // or app.io.of('/')
  app.io.route('chat', app.io.controllers.chat);
};

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:68 (59 by maintainers)

github_iconTop GitHub Comments

4reactions
dead-horsecommented, Feb 14, 2017

如果不混部,为什么要强行在 egg 里面支持 socket.io,我们设计的这一套 web 开发相关的规范本来就美为 socket.io 考虑,等于是要再造一个基于 socket.io 的上层框架,我们本身也没有类似的强需求,不应该做到 egg 里面。

  1. 如果应用真的足够复杂(或者规模上去了),会有单独的 websocket 服务,适合单独部署,也不需要用 egg。
  2. 当应用刚起步,在 web 网站的基础上有一些实时推送的需求,用 egg 开发网站服务,顺便用 egg-socket.io 解决实时推送问题。

我的看法是我们主要解决的第二类问题。

4reactions
calidioncommented, Feb 12, 2017

很好啊。 可以在所有的npm包里都打上egg-前缀,多么美妙的事情啊。哈哈。

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket IO与NIO(二) - 掘金
是一个简单的面向数据报的传输层协议,正式规范为RFC 768。 ... vue-socket.io以及egg-socket.io简单示例WebSocket 是HTML5 开始提供的一种在单个TCP ...
Read more >
Egg 期刊06 - VSCode 调试&& Alinode 接入 - 知乎专栏
对此,@天猪提出了一个RFC 提案,据说类库名有亮点,欢迎围观。 ... 文档地址:egg-socket.io 文档 。 感谢@苏依 同学的贡献。 Θ egg-schedule 优化.
Read more >
Emit An Event To Socket Io In Jmeter - ADocLib
Writing a Socket.IO sampler in JMeter I was recently working on a project with a node.js server and talking to its client through...
Read more >
socket.io连接https - CSDN博客
学习中,后台是express+nodejs,用到socket.io 做聊天功能。 ... 你就知道一些啦,当然有个最好的参考 文档就是HTTP相关的RFC文档,认真读一下肯定解决 ...
Read more >
So what's up | Only Me and Me - WordPress.com
... .dennis.mars.tvs.francis.manga.silent.egg.socket.mhz.caps.literary. ... .sl.confidential.automobile.rfc.statutory.athens.northeast.isp.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found