Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receiving "Callbacks are not support when broadcasting" when using multiple webapps via socket.emit #445

Closed
Lucaszw opened this issue Mar 12, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@Lucaszw
Copy link

Lucaszw commented Mar 12, 2022

We are using socket io callbacks when sending messages to particular connections. (We use heroku for hosting)

Server emit code looks like

const sockets  = await this.deviceSockets.in(channelName).fetchSockets()
for (const socket of sockets) {
socket.emit(eventName, ...args, function(err, result) {
...
});
}

Client code looks like

socket.on('ping', async (callback) => {
callback({ status: 'ok' })
})

This works fine when we only have one web server running; however when we have multiple webservers running (heroku dynos), we receive the following error when our clients try responding to the callbacks:

Callbacks are not support when broadcasting

However we aren't broadcasting (rather emitting to one particular client)

We are configuring socket-io redis in the following way:

const {createAdapter} = require('@socket.io/redis-adapter')
...
const pubClient = redis.createClient({...})
const subClient = this.pubClient.duplicate()
io.adapter(createAdapter(pubClient,subClient)
@darrachequesne
Copy link
Member

Hi! That's indeed a known limitation, acknowledgements across multiple servers are not currently supported. We plan to implement it in the next release though.

Related: socketio/socket.io#1811

darrachequesne added a commit to socketio/socket.io that referenced this issue Mar 31, 2022
Syntax:

```js
io.timeout(1000).emit("some-event", (err, responses) => {
  // ...
});
```

The adapter exposes two additional methods:

- `broadcastWithAck(packets, opts, clientCountCallback, ack)`

Similar to `broadcast(packets, opts)`, but:

* `clientCountCallback()` is called with the number of clients that
  received the packet (can be called several times in a cluster)
* `ack()` is called for each client response

- `serverCount()`

It returns the number of Socket.IO servers in the cluster (1 for the
in-memory adapter).

Those two methods will be implemented in the other adapters (Redis,
Postgres, MongoDB, ...).

Related:

- #1811
- #4163
- socketio/socket.io-redis-adapter#445
darrachequesne added a commit that referenced this issue May 3, 2022
This feature was added in `socket.io@4.5.0`:

```js
io.timeout(1000).emit("some-event", (err, responses) => {
  // ...
});
```

Thanks to this change, it will now work with multiple Socket.IO
servers.

Related:

- socketio/socket.io@8b20457
- #445
- #452
@darrachequesne
Copy link
Member

Implemented in socket.io@4.5.0: https://github.com/socketio/socket.io/releases/tag/4.5.0 🚀

@darrachequesne darrachequesne added the enhancement New feature or request label Dec 7, 2022
dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
Syntax:

```js
io.timeout(1000).emit("some-event", (err, responses) => {
  // ...
});
```

The adapter exposes two additional methods:

- `broadcastWithAck(packets, opts, clientCountCallback, ack)`

Similar to `broadcast(packets, opts)`, but:

* `clientCountCallback()` is called with the number of clients that
  received the packet (can be called several times in a cluster)
* `ack()` is called for each client response

- `serverCount()`

It returns the number of Socket.IO servers in the cluster (1 for the
in-memory adapter).

Those two methods will be implemented in the other adapters (Redis,
Postgres, MongoDB, ...).

Related:

- socketio#1811
- socketio#4163
- socketio/socket.io-redis-adapter#445
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants