Skip to content

Commit

Permalink
feat: protobuf/msgpack support for extended protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Aug 11, 2023
1 parent 4c60aec commit 7f23edf
Show file tree
Hide file tree
Showing 13 changed files with 2,156 additions and 254 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ import { createCable } from '@anycable/web'
export default createCable({protocol: 'actioncable-v1-ext-json'})
```

#### Using with Protobuf and Msgpack

You can use the extended protocol with Protobuf and Msgpack encoders as follows:

```js
// cable.js
import { createCable } from '@anycable/web'
import { MsgpackEncoder } from '@anycable/msgpack-encoder'

export default createCable({protocol: 'actioncable-v1-ext-msgpack', encoder: new MsgpackEncoder()})

// or for protobuf
import { createCable } from '@anycable/web'
import { ProtobufEncoderV2 } from '@anycable/protobuf-encoder'

export default createCable({protocol: 'actioncable-v1-ext-protobuf', encoder: new ProtobufEncoderV2()})
```

#### Loading initial history on client initialization

To catch up messages broadcasted during the initial page load (or client-side application initialization), you can specify the `historyTimestamp` option to retrieve messages after the specified time along with subscription requests. The value must be a UTC timestamp (the number of seconds). For example:
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
"test:dev": "NODE_OPTIONS='--experimental-vm-modules' jest",
"test": "yarn test:dev --coverage && check-dts",
"lint": "eslint . && prettier --check 'packages/**/*.{js,ts}'",
"lint:fix": "eslint . --fix && prettier --check 'packages/**/*.{js,ts}' --write"
"lint:fix": "eslint . --fix && prettier --check 'packages/**/*.{js,ts}' --write",
"protos": "yarn protos:build && yarn protos:fix",
"protos:build": "yarn protos:build:js && yarn protos:build:ts",
"protos:build:js": "pbjs -t static-module --keep-case --force-number -w es6 -o packages/protobuf-encoder/generated/message_pb.js packages/protobuf-encoder/message.proto",
"protos:build:ts": "pbts -o packages/protobuf-encoder/generated/message_pb.d.ts packages/protobuf-encoder/generated/message_pb.js",
"protos:fix": "sed -i '' 's/import \\* as \\$protobuf from \"protobufjs\\/minimal\";/import \\$protobuf from \"protobufjs\\/minimal.js\"\\\n\\$protobuf.util.Long = undefined;\\\n\\$protobuf.configure();/g' packages/protobuf-encoder/generated/message_pb.js"
},
"homepage": "https://anycable.io/",
"repository": "https://github.com/anycable/anycable-client",
Expand Down
1 change: 1 addition & 0 deletions packages/core/cable/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ describe('connect/disconnect', () => {
let p1 = cable.connect()
let p2 = cable.connect()

cable.connected()
cable.connected()

await p1
Expand Down
5 changes: 4 additions & 1 deletion packages/core/create-cable/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { Channel, Message, ChannelParamsMap } from '../channel/index.js'
import { Options } from '../action_cable/index.js'
import { ExtendedOptions } from '../action_cable_ext/index.js'

export type ExtendedProtocolID = 'actioncable-v1-ext-json'
export type ExtendedProtocolID =
| 'actioncable-v1-ext-json'
| 'actioncable-v1-ext-msgpack'
| 'actioncable-v1-ext-protobuf'

export type ProtocolID =
| 'actioncable-v1-json'
Expand Down
4 changes: 4 additions & 0 deletions packages/protobuf-encoder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

## master

- Add `ProtobufEncoderV2` class supporting updated Protobuf schema with Message and Reply types separated.

This encoder MUST be used with the extended protocol.

[@palkan]: https://github.com/palkan
Loading

0 comments on commit 7f23edf

Please sign in to comment.