问题
I'm trying to build a chat applicaton in react native using redis pub/sub. Searched
redis client for javascript but i didn't get. Can anyone let me know how to use redis pub/sub in react native.
回答1:
You have to implement redis on the backend side, not on the frontend. Redis is a database as they say on their websites here
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker
For the frontend side if you want to use publish / subscribe feature you can use websockets for example.
Here is some good libraries to use with React Native:
- Socket.io
- React native also bring support for WebSockets
On the backend side you can use:
- Socket.io
- ws
- sockjs
- socketcluster
Here is an example using the React Native WebSockets:
var ws = new WebSocket('wss://example.com/stuff');
ws.onopen = () => {
// connection opened
ws.send('Hello world'); // send a message
};
ws.onmessage = (e) => {
// a message was received
console.log(e.data);
};
You also have other alternatives, like long-pooling
回答2:
Redis is a database and is often used in backend solutions. As I understand you are trying to implement a client-side solution in React Native.
To make it simpler you can use a ready backend and SDK compatible with React Native to build your app.
For example, ConnectyCube has added React Native support to its JavaScript SDK. It has a lot of features useful for building chat applications for various purposes.
Check their documentation how you can build it.
Here is a useful guide how to build an XMPP chat in React Native.
来源:https://stackoverflow.com/questions/53532428/how-to-implement-publish-subscribe-feature-with-a-server-in-react-native