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

Replacing customHook/customRequest in ^6.0.0 #370

Closed
helderjbe opened this issue Dec 7, 2020 · 11 comments
Closed

Replacing customHook/customRequest in ^6.0.0 #370

helderjbe opened this issue Dec 7, 2020 · 11 comments

Comments

@helderjbe
Copy link

According to the release notes for v6.0.0 in https://socket.io/blog/socket-io-redis-adapter-6-release/:

customHook and customRequest methods were removed Those methods will be replaced by a more intuitive API in a future iteration.

It would be nice to provide an alternative for the devs who were already using customHook and customRequest.

Also, it says that this will be available in a future version. When would that be? Why not simply leave the previous implementation on, as it was already working well?

Great work guys, just wondering about this since I have customHooks implemented in an existing project and would like to upgrade socket io.

@gchokhonelidze
Copy link

You have to write something like this:

	sub.subscribe("MSG");
	nsp.adapter.customRequest = function ({ sender, event, data }) {
		pub.publish("MSG", JSON.stringify({ sender, event, data }));
	};
	sub.on("message", function (channel, message) {
		if (channel !== "MSG") return;
		try {
			const packet = JSON.parse(message);
			//your hook middleware...
		} catch (err) {
			err && err.message && console.log(err.message);
		}
	});
	sub.subscribe("MSG");

@yue4u
Copy link

yue4u commented Mar 12, 2021

Hi! Is there an update on this? Since many pervious issues recommended to use customHook/customRequest to solve common problems like #283 (comment), the removal of these functions is blocking me upgrading to socket.io v3. Should I consider #370 (comment) as the official workaround for now? I'm also curious about when would the future release with a replacement api happen?

@darrachequesne
Copy link
Member

We have added some utility methods in Socket.IO v4 (the major bump is due to a minor breaking change and should not impact most users).

Documentation: https://socket.io/docs/v3/migrating-from-3-x-to-4-0/#Additional-utility-methods

Starting with socket.io-redis@6.1.0, those methods (fetchSockets, socketsJoin, socketsLeave and disconnectSockets) now work with multiple Socket.IO servers.

Does it cover all your use cases?

An user suggested to create another flag, loopback, for inter-server communication. What is your opinion?

@yue4u
Copy link

yue4u commented Mar 13, 2021

@darrachequesne Thank you for the response! The customHook and customRequest methods were more flexible and allowed me perform some extra logic like gathering server status on every node along with node id. The suggested loopback flag seems can be a customHook/customRequest replacement, but as you mentioned there's no way to distinguish between client and server messages.

In general, I want know when handling communications happen only between servers (rpc calls), should I rely on socket.io-redis, or should I add a different redis based rpc layer?

@darrachequesne
Copy link
Member

@rainy-me there is currently no official way to handle communication between servers, but we could indeed add one. I'm open to suggestions on this 🙏

@yue4u
Copy link

yue4u commented Mar 20, 2021

@darrachequesne I mean it was partially doable just via customHook and customRequest, but since they are removed I have to find an alternative. It'll be really helpful to have it in socket.io-redis because we already assign every server node an id and have a mechanism to do communications. Feels redundant to add another rpc layer and may have troubles to associate it to socket.io-redis provided node id. However, I'm also totally fine if it won't be implemented because I don't know if other people are interested. Maybe it's just because my use case is rare.

@martinj
Copy link

martinj commented Mar 29, 2021

I also do some custom things using customHook and customRequest which currently are blocking me from upgrading socket.io

@hmoush95
Copy link

Same here. I'm blocked from upgrading socket.io past v2 because of this removal. customRequest and customHook allowed us to have inter-node communication (i.e. node 1 telling node 2 to do something). The new methods fetchSockets, socketsJoin, socketsLeave and disconnectSockets) unfortunately do not cover all my use-cases.

darrachequesne added a commit to socketio/socket.io that referenced this issue May 10, 2021
Syntax:

```js
// server A
io.serverSideEmit("hello", "world");

// server B
io.on("hello", (arg) => {
  console.log(arg); // prints "world"
});
```

With acknowledgements:

```js
// server A
io.serverSideEmit("hello", "world", (err, responses) => {
  console.log(responses); // prints ["hi"]
});

// server B
io.on("hello", (arg, callback) => {
  callback("hi");
});
```

This feature replaces the customHook/customRequest API from the Redis
adapter: socketio/socket.io-redis-adapter#370
darrachequesne added a commit that referenced this issue May 11, 2021
This new API replaces the customRequest/customHook methods that were
removed in v6.

Syntax:

```js
// server A
io.serverSideEmit("hello", "world");

// server B
io.on("hello", (arg) => {
  console.log(arg); // prints "world"
});
```

With acknowledgements:

```js
// server A
io.serverSideEmit("hello", "world", (err, responses) => {
  console.log(responses); // prints ["hi"]
});

// server B
io.on("hello", (arg, callback) => {
  callback("hi");
});
```

Related: #370
@yue4u
Copy link

yue4u commented Jun 28, 2021

Seems serverSideEmit has been implemented and released! Have not tried it yet but thank you a lot! @darrachequesne

@hmoush95
Copy link

Didn't update the thread. But yes, I was able to upgrade socket.io and use serverSideEmit as a replacement for customeRequest. Works like a charm and has been out in production since last week. Thanks a lot!

@darrachequesne
Copy link
Member

@hmoush95 thanks for the update! 👍

Syntax of the serverSideEmit method, which was added in socket.io@4.1.0:

io.serverSideEmit("hello", "world");

// on the other nodes
io.on("hello", (arg1) => {
  console.log(arg1); // prints "world"
});

Let's close this now.

dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
Syntax:

```js
// server A
io.serverSideEmit("hello", "world");

// server B
io.on("hello", (arg) => {
  console.log(arg); // prints "world"
});
```

With acknowledgements:

```js
// server A
io.serverSideEmit("hello", "world", (err, responses) => {
  console.log(responses); // prints ["hi"]
});

// server B
io.on("hello", (arg, callback) => {
  callback("hi");
});
```

This feature replaces the customHook/customRequest API from the Redis
adapter: socketio/socket.io-redis-adapter#370
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants