Skip to content

Commit

Permalink
use a single socket-io endpoint (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Wang <[email protected]>
  • Loading branch information
tigerinus authored Jan 11, 2023
1 parent 2e9ce83 commit 95ba219
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 187 deletions.
115 changes: 70 additions & 45 deletions api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ tags:
description: |-
methods for managing action types, as well as publishing and subscribing to actions
- name: Websocket Endpoints
description: |-
Endpoint paths for subscribing to events and actions via WebSocket
- name: SocketIO Endpoints
description: |-
Endpoint paths for subscribing to events and actions via SocketIO
- name: EventType
description: |-
<SchemaDefinition schemaRef="#/components/schemas/EventType" />
Expand All @@ -42,6 +50,10 @@ tags:
description: |-
<SchemaDefinition schemaRef="#/components/schemas/Action" />
- name: PropertyType
description: |-
<SchemaDefinition schemaRef="#/components/schemas/PropertyType" />
x-tagGroups:
- name: Methods
tags:
Expand All @@ -50,12 +62,18 @@ x-tagGroups:
- ActionType methods
- Action methods

- name: Endpoints
tags:
- WebSocket Endpoints
- SocketIO Endpoints

- name: Schemas
tags:
- EventType
- Event
- ActionType
- Action
- PropertyType

security:
- access_token: []
Expand Down Expand Up @@ -155,7 +173,7 @@ paths:
> `names` can be specified in the query string to subscribe to specific event types.
operationId: subscribeEventWS
tags:
- Event methods
- WebSocket Endpoints
parameters:
- $ref: "#/components/parameters/SourceID"
- $ref: "#/components/parameters/EventNames"
Expand All @@ -164,34 +182,6 @@ paths:
description: |
The connection will be upgraded to a WebSocket connection and the client will receive events as they are published.
/event:
get:
summary: Subscribe to events by source ID (SocketIO)
description: |
Subscribe to events by `source_id` via SocketIO.
> `source_id` should be specified as the SocketIO *event* (which is a different from *event* in message bus) when listening.
operationId: subscribeEventSIO
tags:
- Event methods
responses:
"101":
description: |
The connection will be upgraded to a SocketIO connection and the client will receive events as they are published.
/event/:
get:
summary: Subscribe to all event types (SocketIO)
description: |
Subscribe to all event types via SocketIO.
> In URI definition, `/event` and `/event/` are different paths. This endpoint is a workaround to maximize compatibility.
operationId: subscribeEventSIO2
responses:
"101":
description: |
The connection will be upgraded to a SocketIO connection and the client will receive events as they are published.
/action_type:
get:
summary: List action types
Expand Down Expand Up @@ -284,7 +274,7 @@ paths:
> `names` can be specified in the query string to subscribe to specific action types.
operationId: subscribeActionWS
tags:
- Action methods
- WebSocket Endpoints
parameters:
- $ref: "#/components/parameters/SourceID"
- $ref: "#/components/parameters/ActionNames"
Expand All @@ -293,32 +283,60 @@ paths:
description: |
The connection will be upgraded to a WebSocket connection and the client will receive actions as they are triggered.
/action:
/socket.io:
get:
summary: Subscribe to actions by source ID (SocketIO)
summary: Subscribe to events and actions (SocketIO)
description: |
Subscribe to actions by `source_id` via SocketIO.
> `source_id` should be specified as the SocketIO *event* (which is a different from *event* in message bus) when listening.
operationId: subscribeActionSIO
Subscribe to events and actions via SocketIO.
- SocketIO `room` should be either `event` or `action`
operationId: subscribeSIO
tags:
- Action methods
- SocketIO Endpoints
responses:
"101":
description: |
The connection will be upgraded to a SocketIO connection and the client will receive actions as they are triggered.
The connection will be upgraded to a SocketIO connection and the client will receive events as they are published.
"200":
description: |
Polling initialized for SocketIO
/action/:
post:
summary: Poll events and actions (SocketIO)
description: |
Poll events and actions by via SocketIO.
operationId: pollSIO
tags:
- SocketIO Endpoints
responses:
"200":
description: |
Polling continued from SocketIO
/socket.io/:
get:
summary: Subscribe to all action types (SocketIO)
summary: Subscribe to events and actions (SocketIO)
description: |
Subscribe to all action types via SocketIO.
> In URI definition, `/action` and `/action/` are different paths. This endpoint is a workaround to maximize compatibility.
operationId: subscribeActionSIO2
> Same as `/socket.io` except it comes with a trailing slash `/` - a workaround to maximize compatibility
operationId: subscribeSIO2
responses:
"101":
description: |
The connection will be upgraded to a SocketIO connection and the client will receive actions as they are triggered.
The connection will be upgraded to a SocketIO connection and the client will receive events as they are published.
"200":
description: |
Polling initialized for SocketIO
post:
summary: Poll events and actions (SocketIO)
description: |
> Same as `/socket.io` except it comes with a trailing slash `/` - a workaround to maximize compatibility
operationId: pollSIO2
responses:
"200":
description: |
Polling continued from SocketIO
components:
securitySchemes:
Expand Down Expand Up @@ -527,7 +545,14 @@ components:
properties:
name:
type: string
description: property name
description: |-
property name
> It is recommended for a property name to be as descriptive as possible. One option is to prefix with a namespace.
> - If the property is source specific, prefix with source ID. For example, `local-storage:vendor`
> - Otherwise, prefix with `common:`. For example, `common:email`
>
> Some bad examples are `id`, `avail`, `blk`...which can be ambiguous and confusing.
example: "local-storage:vendor"
description:
type: string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomodule/redigo v1.8.4 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v1.8.4 h1:Z5JUg94HMTR1XpwBaSH4vq3+PNSIykBLxMdglbw10gg=
github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
Expand Down
13 changes: 2 additions & 11 deletions route/api_route_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func (r *APIRoute) TriggerAction(c echo.Context, sourceID codegen.SourceID, name
Timestamp: utils.Ptr(time.Now()),
}

go r.services.SocketIOService.Publish(in.ActionAdapter(action))

result, err := r.services.ActionServiceWS.Trigger(in.ActionAdapter(action))
if err != nil {
message := err.Error()
Expand Down Expand Up @@ -199,14 +201,3 @@ func (r *APIRoute) SubscribeActionWS(c echo.Context, sourceID codegen.SourceID,

return nil
}

func (r *APIRoute) SubscribeActionSIO(ctx echo.Context) error {
server := r.services.ActionServiceSIO.Server()
server.ServeHTTP(ctx.Response(), ctx.Request())
return nil
}

// unfortunately need to duplicate this func to support both `/action` and `/action/` API endpoints
func (r *APIRoute) SubscribeActionSIO2(ctx echo.Context) error {
return r.SubscribeActionSIO(ctx)
}
13 changes: 1 addition & 12 deletions route/api_route_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam
Uuid: &uuidStr,
}

go r.services.EventServiceSIO.Publish(in.EventAdapter(event))
go r.services.SocketIOService.Publish(in.EventAdapter(event))

result, err := r.services.EventServiceWS.Publish(in.EventAdapter(event))
if err != nil {
Expand Down Expand Up @@ -217,14 +217,3 @@ func (r *APIRoute) SubscribeEventWS(c echo.Context, sourceID codegen.SourceID, p

return nil
}

func (r *APIRoute) SubscribeEventSIO(ctx echo.Context) error {
server := r.services.EventServiceSIO.Server()
server.ServeHTTP(ctx.Response(), ctx.Request())
return nil
}

// unfortunately need to duplicate the func to support both `/event` and `/event/` API endpoints
func (r *APIRoute) SubscribeEventSIO2(ctx echo.Context) error {
return r.SubscribeEventSIO(ctx)
}
25 changes: 25 additions & 0 deletions route/api_route_socketio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package route

import "github.com/labstack/echo/v4"

func (r *APIRoute) SubscribeSIO(ctx echo.Context) error {
server := r.services.SocketIOService.Server()
server.ServeHTTP(ctx.Response(), ctx.Request())
return nil
}

// unfortunately need to duplicate the func to support both `/socket.io` and `/socket.io/` (with a trailing slash) API endpoints
func (r *APIRoute) SubscribeSIO2(ctx echo.Context) error {
return r.SubscribeSIO(ctx)
}

func (r *APIRoute) PollSIO(ctx echo.Context) error {
server := r.services.SocketIOService.Server()
server.ServeHTTP(ctx.Response(), ctx.Request())
return nil
}

// unfortunately need to duplicate the func to support both `/socket.io` and `/socket.io/` (with a trailing slash) API endpoints
func (r *APIRoute) PollSIO2(ctx echo.Context) error {
return r.PollSIO(ctx)
}
34 changes: 0 additions & 34 deletions service/action_service_socketio.go

This file was deleted.

34 changes: 0 additions & 34 deletions service/event_service_socketio.go

This file was deleted.

Loading

0 comments on commit 95ba219

Please sign in to comment.