2

I have a Laravel php app, and a NodeJS client. Something happens in my client and I want to update my php backend, so I publish a message to a Redis channel, and in my Laravel app I have a Redis subscriber listening for messages on that channel.

Ideally, I want to fire an event when a message is received but I get this error

[Predis\Response\ServerException] ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context

I am able to do what I need from within the subscriber, like updating the repository etc... but I am unable to fire a Laravel event, which is what it makes sense to do here.

  • 1
    Matthew Lilley wrote a solution for this on laracast: https://laracasts.com/discuss/channels/laravel/unable-to-fire-a-laravel-event-from-within-a-laravel-redis-pubsub-subscriber-only-happens-when-im-interacting-with-queues?page=0 – Sangar82 May 19 '16 at 12:57

1 Answers1

2

You are required to use two connections for pub and sub. A subscriber connection cannot issue any commands other than subscribe, psubscribe, unsubscribe, punsubscribe (although @Antirez has hinted of a subscriber-safe ping in the future). If you try to do anything else, redis tells you:

-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context (note that you can't test this with redis-cli, since that understands the protocol well enough to prevent you from issuing commands once you have subscribed - but any other basic socket tool should work fine)

This is because subscriber connections work very differently - rather than working on a request/response basis, incoming messages can now come in at any time, unsolicited.

publish is a regular request/response command, so must be sent on a regular connection, not a subscriber connection. For more information click here

Jigs Virani
  • 4,067
  • 1
  • 23
  • 24
  • My Node application is the publisher. My Laravel application is the subscriber. Laravel subscribed to the channel, node has published to. This is how the pub/sub model works, correct? – Matthew Lilley Jul 10 '15 at 06:21
  • When the subscriber receives a message, I need to fire a Laravel event. – Matthew Lilley Jul 10 '15 at 06:21
  • yes exactly.when sub rec. a message you have to need inform laravel means fire a Laravel event in event.php and also handle in handle class – Jigs Virani Jul 10 '15 at 06:24
  • This is my problem. I couldn't fire an event. I had to save the data to a private variable, unsubscribe to that channel and then fire the Laravel event. – Matthew Lilley Jul 12 '15 at 03:04