# 分析 Chinese dev mode 和 Global dev mode
###### tags: `cli`
Dev mode 分为两部分:
### CLI 前端部分
Global 和 Chinese 这边在cli的这部分的逻辑基本相同:
1. 监听项目文件变化
2. 变化之后出发 `deploy`
3. 建立 **websocket** 连接,得到主动推送的功能
第三步建立连接这里有不同:
1. Chinese: 直接通过腾讯提供的[utils-china包中的方法建立ws连接](https://github.com/serverless/components/blob/65fccf543153407fbc2b855bfa777433408e0430/src/cli/commands-cn/dev.js#L114), [sdk中的封装](https://github.com/serverlessinc/platform-cn/blob/2aa2292c89550dd801a4a411b61f862153c7d9a1/sdk/src/utils.js#L568)
2. Global: [通过SDK的connect方法建立的ws连接](https://github.com/serverless/components/blob/65fccf543153407fbc2b855bfa777433408e0430/src/cli/commands/dev.js#L221), [sdk的connect文档](https://github.com/serverlessinc/platform/tree/master/sdk#serverlesssdkconnectoptions--null)
### Dev 后端部分
1. Chinese: 通过腾讯[utils-china]中的方法,直接调用腾讯的api建立ws连接,进行服务端的dev 日志推送: https://github.com/serverlessinc/platform-cn/blob/2aa2292c89550dd801a4a411b61f862153c7d9a1/sdk/src/utils.js#L568
2. Global:cli调用sdk中的`connect` 方法,建立ws连接:https://github.com/serverlessinc/platform/tree/master/sdk#serverlesssdkconnectoptions--null,
1. connect方法通过参数构建 WS URL, 建立Events的websocket 连接: https://github.com/serverlessinc/platform/blob/593e41f98dc013053e414b2b699c81e12bb03c7c/sdk/src/Connection.js#L23
2. 连接到 Events 服务,events 服务提供ws主动推送:https://github.com/serverlessinc/platform/blob/593e41f98dc013053e414b2b699c81e12bb03c7c/sp-events/code/src/handler.js#L212
### 这是我询问 Eslam 关于 Global dev mode的架构设计,他给的答复:
> Hey Meng, unfortunately we don’t have such doc. Not sure how much details you need, but to sum it up:
> 1. on dev mode, we connect via websockets to our platfrom event bus backend by providing certain context information (orgName, serviceName..etc)
> 2. during first deployment, we inject our own SDK into the user’s lambda as a middleware that runs on every lambda invocation
> 3. on every lambda invocation during dev mode, the SDK has an async timer than runs every 100ms or something that collections all the logs the user logged and patch-request them to our platform event bus backend along with context information (orgName, serviceName…etc)p
> 4. our platform event bus backend receives the events and pushes them over the websocket connect established with the CLI according to the context information, and this is how the CLI receives the logs
> 5. the watching feature is pretty straightforward. We just watch the directory and redeploy on changes
我根据global的代码对于上面Eslam说的这几步梳理了一下代码所在:
1. https://github.com/serverless/components/blob/65fccf543153407fbc2b855bfa777433408e0430/src/cli/commands/dev.js#L221: connect方法调用,建立cli和event bus的ws连接
2. [deploy的时候添加dev: true Option](https://github.com/serverless/components/blob/6e485ae27f9b38c8694b34c0e7dc95d962cb7f4c/src/cli/commands/dev.js#L278), 最终这个字段会注入到[addSDK](https://github.com/serverlessinc/platform/blob/593e41f98dc013053e414b2b699c81e12bb03c7c/sp-registry/layer/src/handler.js#L167)这个方法中,完成sdk的注入
3. Lambda 函数执行的时候,sdk会向我们的event bus 发送请求: https://github.com/serverlessinc/platform/blob/593e41f98dc013053e414b2b699c81e12bb03c7c/sp-events/code/src/handler.js#L212
4. Evnet bus 收到请求,会根据context information筛选信息,然后通过第一步建立的ws连接。推送到cli: https://github.com/serverlessinc/platform/blob/593e41f98dc013053e414b2b699c81e12bb03c7c/sp-events/code/src/handler.js#L99
5. 这一步是监听用户文件然后触发重新部署,没有特殊的地方